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 <asm/unaligned.h>
  58
  59#include "mpt3sas_base.h"
  60
  61#define RAID_CHANNEL 1
  62/* forward proto's */
  63static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  64        struct _sas_node *sas_expander);
  65static void _firmware_event_work(struct work_struct *work);
  66
  67static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  68        struct _sas_device *sas_device);
  69static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  70        u8 retry_count, u8 is_pd);
  71
  72static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
  73
  74/* global parameters */
  75LIST_HEAD(mpt3sas_ioc_list);
  76/* global ioc lock for list operations */
  77DEFINE_SPINLOCK(gioc_lock);
  78
  79MODULE_AUTHOR(MPT3SAS_AUTHOR);
  80MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
  81MODULE_LICENSE("GPL");
  82MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
  83MODULE_ALIAS("mpt2sas");
  84
  85/* local parameters */
  86static u8 scsi_io_cb_idx = -1;
  87static u8 tm_cb_idx = -1;
  88static u8 ctl_cb_idx = -1;
  89static u8 base_cb_idx = -1;
  90static u8 port_enable_cb_idx = -1;
  91static u8 transport_cb_idx = -1;
  92static u8 scsih_cb_idx = -1;
  93static u8 config_cb_idx = -1;
  94static int mpt2_ids;
  95static int mpt3_ids;
  96
  97static u8 tm_tr_cb_idx = -1 ;
  98static u8 tm_tr_volume_cb_idx = -1 ;
  99static u8 tm_sas_control_cb_idx = -1;
 100
 101/* command line options */
 102static u32 logging_level;
 103MODULE_PARM_DESC(logging_level,
 104        " bits for enabling additional logging info (default=0)");
 105
 106
 107static ushort max_sectors = 0xFFFF;
 108module_param(max_sectors, ushort, 0);
 109MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
 110
 111
 112static int missing_delay[2] = {-1, -1};
 113module_param_array(missing_delay, int, NULL, 0);
 114MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
 115
 116/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
 117#define MPT3SAS_MAX_LUN (16895)
 118static u64 max_lun = MPT3SAS_MAX_LUN;
 119module_param(max_lun, ullong, 0);
 120MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
 121
 122static ushort hbas_to_enumerate;
 123module_param(hbas_to_enumerate, ushort, 0);
 124MODULE_PARM_DESC(hbas_to_enumerate,
 125                " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
 126                  1 - enumerates only SAS 2.0 generation HBAs\n \
 127                  2 - enumerates only SAS 3.0 generation HBAs (default=0)");
 128
 129/* diag_buffer_enable is bitwise
 130 * bit 0 set = TRACE
 131 * bit 1 set = SNAPSHOT
 132 * bit 2 set = EXTENDED
 133 *
 134 * Either bit can be set, or both
 135 */
 136static int diag_buffer_enable = -1;
 137module_param(diag_buffer_enable, int, 0);
 138MODULE_PARM_DESC(diag_buffer_enable,
 139        " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
 140static int disable_discovery = -1;
 141module_param(disable_discovery, int, 0);
 142MODULE_PARM_DESC(disable_discovery, " disable discovery ");
 143
 144
 145/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
 146static int prot_mask = -1;
 147module_param(prot_mask, int, 0);
 148MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
 149
 150
 151/* raid transport support */
 152struct raid_template *mpt3sas_raid_template;
 153struct raid_template *mpt2sas_raid_template;
 154
 155
 156/**
 157 * struct sense_info - common structure for obtaining sense keys
 158 * @skey: sense key
 159 * @asc: additional sense code
 160 * @ascq: additional sense code qualifier
 161 */
 162struct sense_info {
 163        u8 skey;
 164        u8 asc;
 165        u8 ascq;
 166};
 167
 168#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
 169#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
 170#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
 171#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
 172#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
 173/**
 174 * struct fw_event_work - firmware event struct
 175 * @list: link list framework
 176 * @work: work object (ioc->fault_reset_work_q)
 177 * @cancel_pending_work: flag set during reset handling
 178 * @ioc: per adapter object
 179 * @device_handle: device handle
 180 * @VF_ID: virtual function id
 181 * @VP_ID: virtual port id
 182 * @ignore: flag meaning this event has been marked to ignore
 183 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
 184 * @event_data: reply event data payload follows
 185 *
 186 * This object stored on ioc->fw_event_list.
 187 */
 188struct fw_event_work {
 189        struct list_head        list;
 190        struct work_struct      work;
 191        u8                      cancel_pending_work;
 192        struct delayed_work     delayed_work;
 193
 194        struct MPT3SAS_ADAPTER *ioc;
 195        u16                     device_handle;
 196        u8                      VF_ID;
 197        u8                      VP_ID;
 198        u8                      ignore;
 199        u16                     event;
 200        struct kref             refcount;
 201        char                    event_data[0] __aligned(4);
 202};
 203
 204static void fw_event_work_free(struct kref *r)
 205{
 206        kfree(container_of(r, struct fw_event_work, refcount));
 207}
 208
 209static void fw_event_work_get(struct fw_event_work *fw_work)
 210{
 211        kref_get(&fw_work->refcount);
 212}
 213
 214static void fw_event_work_put(struct fw_event_work *fw_work)
 215{
 216        kref_put(&fw_work->refcount, fw_event_work_free);
 217}
 218
 219static struct fw_event_work *alloc_fw_event_work(int len)
 220{
 221        struct fw_event_work *fw_event;
 222
 223        fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
 224        if (!fw_event)
 225                return NULL;
 226
 227        kref_init(&fw_event->refcount);
 228        return fw_event;
 229}
 230
 231/**
 232 * struct _scsi_io_transfer - scsi io transfer
 233 * @handle: sas device handle (assigned by firmware)
 234 * @is_raid: flag set for hidden raid components
 235 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
 236 * @data_length: data transfer length
 237 * @data_dma: dma pointer to data
 238 * @sense: sense data
 239 * @lun: lun number
 240 * @cdb_length: cdb length
 241 * @cdb: cdb contents
 242 * @timeout: timeout for this command
 243 * @VF_ID: virtual function id
 244 * @VP_ID: virtual port id
 245 * @valid_reply: flag set for reply message
 246 * @sense_length: sense length
 247 * @ioc_status: ioc status
 248 * @scsi_state: scsi state
 249 * @scsi_status: scsi staus
 250 * @log_info: log information
 251 * @transfer_length: data length transfer when there is a reply message
 252 *
 253 * Used for sending internal scsi commands to devices within this module.
 254 * Refer to _scsi_send_scsi_io().
 255 */
 256struct _scsi_io_transfer {
 257        u16     handle;
 258        u8      is_raid;
 259        enum dma_data_direction dir;
 260        u32     data_length;
 261        dma_addr_t data_dma;
 262        u8      sense[SCSI_SENSE_BUFFERSIZE];
 263        u32     lun;
 264        u8      cdb_length;
 265        u8      cdb[32];
 266        u8      timeout;
 267        u8      VF_ID;
 268        u8      VP_ID;
 269        u8      valid_reply;
 270  /* the following bits are only valid when 'valid_reply = 1' */
 271        u32     sense_length;
 272        u16     ioc_status;
 273        u8      scsi_state;
 274        u8      scsi_status;
 275        u32     log_info;
 276        u32     transfer_length;
 277};
 278
 279/**
 280 * _scsih_set_debug_level - global setting of ioc->logging_level.
 281 *
 282 * Note: The logging levels are defined in mpt3sas_debug.h.
 283 */
 284static int
 285_scsih_set_debug_level(const char *val, struct kernel_param *kp)
 286{
 287        int ret = param_set_int(val, kp);
 288        struct MPT3SAS_ADAPTER *ioc;
 289
 290        if (ret)
 291                return ret;
 292
 293        pr_info("setting logging_level(0x%08x)\n", logging_level);
 294        spin_lock(&gioc_lock);
 295        list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
 296                ioc->logging_level = logging_level;
 297        spin_unlock(&gioc_lock);
 298        return 0;
 299}
 300module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
 301        &logging_level, 0644);
 302
 303/**
 304 * _scsih_srch_boot_sas_address - search based on sas_address
 305 * @sas_address: sas address
 306 * @boot_device: boot device object from bios page 2
 307 *
 308 * Returns 1 when there's a match, 0 means no match.
 309 */
 310static inline int
 311_scsih_srch_boot_sas_address(u64 sas_address,
 312        Mpi2BootDeviceSasWwid_t *boot_device)
 313{
 314        return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
 315}
 316
 317/**
 318 * _scsih_srch_boot_device_name - search based on device name
 319 * @device_name: device name specified in INDENTIFY fram
 320 * @boot_device: boot device object from bios page 2
 321 *
 322 * Returns 1 when there's a match, 0 means no match.
 323 */
 324static inline int
 325_scsih_srch_boot_device_name(u64 device_name,
 326        Mpi2BootDeviceDeviceName_t *boot_device)
 327{
 328        return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
 329}
 330
 331/**
 332 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
 333 * @enclosure_logical_id: enclosure logical id
 334 * @slot_number: slot number
 335 * @boot_device: boot device object from bios page 2
 336 *
 337 * Returns 1 when there's a match, 0 means no match.
 338 */
 339static inline int
 340_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
 341        Mpi2BootDeviceEnclosureSlot_t *boot_device)
 342{
 343        return (enclosure_logical_id == le64_to_cpu(boot_device->
 344            EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
 345            SlotNumber)) ? 1 : 0;
 346}
 347
 348/**
 349 * _scsih_is_boot_device - search for matching boot device.
 350 * @sas_address: sas address
 351 * @device_name: device name specified in INDENTIFY fram
 352 * @enclosure_logical_id: enclosure logical id
 353 * @slot_number: slot number
 354 * @form: specifies boot device form
 355 * @boot_device: boot device object from bios page 2
 356 *
 357 * Returns 1 when there's a match, 0 means no match.
 358 */
 359static int
 360_scsih_is_boot_device(u64 sas_address, u64 device_name,
 361        u64 enclosure_logical_id, u16 slot, u8 form,
 362        Mpi2BiosPage2BootDevice_t *boot_device)
 363{
 364        int rc = 0;
 365
 366        switch (form) {
 367        case MPI2_BIOSPAGE2_FORM_SAS_WWID:
 368                if (!sas_address)
 369                        break;
 370                rc = _scsih_srch_boot_sas_address(
 371                    sas_address, &boot_device->SasWwid);
 372                break;
 373        case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
 374                if (!enclosure_logical_id)
 375                        break;
 376                rc = _scsih_srch_boot_encl_slot(
 377                    enclosure_logical_id,
 378                    slot, &boot_device->EnclosureSlot);
 379                break;
 380        case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
 381                if (!device_name)
 382                        break;
 383                rc = _scsih_srch_boot_device_name(
 384                    device_name, &boot_device->DeviceName);
 385                break;
 386        case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
 387                break;
 388        }
 389
 390        return rc;
 391}
 392
 393/**
 394 * _scsih_get_sas_address - set the sas_address for given device handle
 395 * @handle: device handle
 396 * @sas_address: sas address
 397 *
 398 * Returns 0 success, non-zero when failure
 399 */
 400static int
 401_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
 402        u64 *sas_address)
 403{
 404        Mpi2SasDevicePage0_t sas_device_pg0;
 405        Mpi2ConfigReply_t mpi_reply;
 406        u32 ioc_status;
 407
 408        *sas_address = 0;
 409
 410        if (handle <= ioc->sas_hba.num_phys) {
 411                *sas_address = ioc->sas_hba.sas_address;
 412                return 0;
 413        }
 414
 415        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
 416            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
 417                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
 418                __FILE__, __LINE__, __func__);
 419                return -ENXIO;
 420        }
 421
 422        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
 423        if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
 424                *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
 425                return 0;
 426        }
 427
 428        /* we hit this becuase the given parent handle doesn't exist */
 429        if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
 430                return -ENXIO;
 431
 432        /* else error case */
 433        pr_err(MPT3SAS_FMT
 434                "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
 435                ioc->name, handle, ioc_status,
 436             __FILE__, __LINE__, __func__);
 437        return -EIO;
 438}
 439
 440/**
 441 * _scsih_determine_boot_device - determine boot device.
 442 * @ioc: per adapter object
 443 * @device: either sas_device or raid_device object
 444 * @is_raid: [flag] 1 = raid object, 0 = sas object
 445 *
 446 * Determines whether this device should be first reported device to
 447 * to scsi-ml or sas transport, this purpose is for persistent boot device.
 448 * There are primary, alternate, and current entries in bios page 2. The order
 449 * priority is primary, alternate, then current.  This routine saves
 450 * the corresponding device object and is_raid flag in the ioc object.
 451 * The saved data to be used later in _scsih_probe_boot_devices().
 452 */
 453static void
 454_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
 455        void *device, u8 is_raid)
 456{
 457        struct _sas_device *sas_device;
 458        struct _raid_device *raid_device;
 459        u64 sas_address;
 460        u64 device_name;
 461        u64 enclosure_logical_id;
 462        u16 slot;
 463
 464         /* only process this function when driver loads */
 465        if (!ioc->is_driver_loading)
 466                return;
 467
 468         /* no Bios, return immediately */
 469        if (!ioc->bios_pg3.BiosVersion)
 470                return;
 471
 472        if (!is_raid) {
 473                sas_device = device;
 474                sas_address = sas_device->sas_address;
 475                device_name = sas_device->device_name;
 476                enclosure_logical_id = sas_device->enclosure_logical_id;
 477                slot = sas_device->slot;
 478        } else {
 479                raid_device = device;
 480                sas_address = raid_device->wwid;
 481                device_name = 0;
 482                enclosure_logical_id = 0;
 483                slot = 0;
 484        }
 485
 486        if (!ioc->req_boot_device.device) {
 487                if (_scsih_is_boot_device(sas_address, device_name,
 488                    enclosure_logical_id, slot,
 489                    (ioc->bios_pg2.ReqBootDeviceForm &
 490                    MPI2_BIOSPAGE2_FORM_MASK),
 491                    &ioc->bios_pg2.RequestedBootDevice)) {
 492                        dinitprintk(ioc, pr_info(MPT3SAS_FMT
 493                           "%s: req_boot_device(0x%016llx)\n",
 494                            ioc->name, __func__,
 495                            (unsigned long long)sas_address));
 496                        ioc->req_boot_device.device = device;
 497                        ioc->req_boot_device.is_raid = is_raid;
 498                }
 499        }
 500
 501        if (!ioc->req_alt_boot_device.device) {
 502                if (_scsih_is_boot_device(sas_address, device_name,
 503                    enclosure_logical_id, slot,
 504                    (ioc->bios_pg2.ReqAltBootDeviceForm &
 505                    MPI2_BIOSPAGE2_FORM_MASK),
 506                    &ioc->bios_pg2.RequestedAltBootDevice)) {
 507                        dinitprintk(ioc, pr_info(MPT3SAS_FMT
 508                           "%s: req_alt_boot_device(0x%016llx)\n",
 509                            ioc->name, __func__,
 510                            (unsigned long long)sas_address));
 511                        ioc->req_alt_boot_device.device = device;
 512                        ioc->req_alt_boot_device.is_raid = is_raid;
 513                }
 514        }
 515
 516        if (!ioc->current_boot_device.device) {
 517                if (_scsih_is_boot_device(sas_address, device_name,
 518                    enclosure_logical_id, slot,
 519                    (ioc->bios_pg2.CurrentBootDeviceForm &
 520                    MPI2_BIOSPAGE2_FORM_MASK),
 521                    &ioc->bios_pg2.CurrentBootDevice)) {
 522                        dinitprintk(ioc, pr_info(MPT3SAS_FMT
 523                           "%s: current_boot_device(0x%016llx)\n",
 524                            ioc->name, __func__,
 525                            (unsigned long long)sas_address));
 526                        ioc->current_boot_device.device = device;
 527                        ioc->current_boot_device.is_raid = is_raid;
 528                }
 529        }
 530}
 531
 532static struct _sas_device *
 533__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 534                struct MPT3SAS_TARGET *tgt_priv)
 535{
 536        struct _sas_device *ret;
 537
 538        assert_spin_locked(&ioc->sas_device_lock);
 539
 540        ret = tgt_priv->sdev;
 541        if (ret)
 542                sas_device_get(ret);
 543
 544        return ret;
 545}
 546
 547static struct _sas_device *
 548mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
 549                struct MPT3SAS_TARGET *tgt_priv)
 550{
 551        struct _sas_device *ret;
 552        unsigned long flags;
 553
 554        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 555        ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
 556        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 557
 558        return ret;
 559}
 560
 561
 562struct _sas_device *
 563__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
 564                                        u64 sas_address)
 565{
 566        struct _sas_device *sas_device;
 567
 568        assert_spin_locked(&ioc->sas_device_lock);
 569
 570        list_for_each_entry(sas_device, &ioc->sas_device_list, list)
 571                if (sas_device->sas_address == sas_address)
 572                        goto found_device;
 573
 574        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
 575                if (sas_device->sas_address == sas_address)
 576                        goto found_device;
 577
 578        return NULL;
 579
 580found_device:
 581        sas_device_get(sas_device);
 582        return sas_device;
 583}
 584
 585/**
 586 * mpt3sas_get_sdev_by_addr - sas device search
 587 * @ioc: per adapter object
 588 * @sas_address: sas address
 589 * Context: Calling function should acquire ioc->sas_device_lock
 590 *
 591 * This searches for sas_device based on sas_address, then return sas_device
 592 * object.
 593 */
 594struct _sas_device *
 595mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
 596        u64 sas_address)
 597{
 598        struct _sas_device *sas_device;
 599        unsigned long flags;
 600
 601        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 602        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
 603                        sas_address);
 604        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 605
 606        return sas_device;
 607}
 608
 609static struct _sas_device *
 610__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 611{
 612        struct _sas_device *sas_device;
 613
 614        assert_spin_locked(&ioc->sas_device_lock);
 615
 616        list_for_each_entry(sas_device, &ioc->sas_device_list, list)
 617                if (sas_device->handle == handle)
 618                        goto found_device;
 619
 620        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
 621                if (sas_device->handle == handle)
 622                        goto found_device;
 623
 624        return NULL;
 625
 626found_device:
 627        sas_device_get(sas_device);
 628        return sas_device;
 629}
 630
 631/**
 632 * mpt3sas_get_sdev_by_handle - sas device search
 633 * @ioc: per adapter object
 634 * @handle: sas device handle (assigned by firmware)
 635 * Context: Calling function should acquire ioc->sas_device_lock
 636 *
 637 * This searches for sas_device based on sas_address, then return sas_device
 638 * object.
 639 */
 640static struct _sas_device *
 641mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 642{
 643        struct _sas_device *sas_device;
 644        unsigned long flags;
 645
 646        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 647        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
 648        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 649
 650        return sas_device;
 651}
 652
 653/**
 654 * _scsih_sas_device_remove - remove sas_device from list.
 655 * @ioc: per adapter object
 656 * @sas_device: the sas_device object
 657 * Context: This function will acquire ioc->sas_device_lock.
 658 *
 659 * If sas_device is on the list, remove it and decrement its reference count.
 660 */
 661static void
 662_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
 663        struct _sas_device *sas_device)
 664{
 665        unsigned long flags;
 666
 667        if (!sas_device)
 668                return;
 669        pr_info(MPT3SAS_FMT
 670            "removing handle(0x%04x), sas_addr(0x%016llx)\n",
 671            ioc->name, sas_device->handle,
 672            (unsigned long long) sas_device->sas_address);
 673
 674        if (sas_device->enclosure_handle != 0)
 675                pr_info(MPT3SAS_FMT
 676                   "removing enclosure logical id(0x%016llx), slot(%d)\n",
 677                   ioc->name, (unsigned long long)
 678                   sas_device->enclosure_logical_id, sas_device->slot);
 679
 680        if (sas_device->connector_name[0] != '\0')
 681                pr_info(MPT3SAS_FMT
 682                   "removing enclosure level(0x%04x), connector name( %s)\n",
 683                   ioc->name, sas_device->enclosure_level,
 684                   sas_device->connector_name);
 685
 686        /*
 687         * The lock serializes access to the list, but we still need to verify
 688         * that nobody removed the entry while we were waiting on the lock.
 689         */
 690        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 691        if (!list_empty(&sas_device->list)) {
 692                list_del_init(&sas_device->list);
 693                sas_device_put(sas_device);
 694        }
 695        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 696}
 697
 698/**
 699 * _scsih_device_remove_by_handle - removing device object by handle
 700 * @ioc: per adapter object
 701 * @handle: device handle
 702 *
 703 * Return nothing.
 704 */
 705static void
 706_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 707{
 708        struct _sas_device *sas_device;
 709        unsigned long flags;
 710
 711        if (ioc->shost_recovery)
 712                return;
 713
 714        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 715        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
 716        if (sas_device) {
 717                list_del_init(&sas_device->list);
 718                sas_device_put(sas_device);
 719        }
 720        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 721        if (sas_device) {
 722                _scsih_remove_device(ioc, sas_device);
 723                sas_device_put(sas_device);
 724        }
 725}
 726
 727/**
 728 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
 729 * @ioc: per adapter object
 730 * @sas_address: device sas_address
 731 *
 732 * Return nothing.
 733 */
 734void
 735mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
 736        u64 sas_address)
 737{
 738        struct _sas_device *sas_device;
 739        unsigned long flags;
 740
 741        if (ioc->shost_recovery)
 742                return;
 743
 744        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 745        sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
 746        if (sas_device) {
 747                list_del_init(&sas_device->list);
 748                sas_device_put(sas_device);
 749        }
 750        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 751        if (sas_device) {
 752                _scsih_remove_device(ioc, sas_device);
 753                sas_device_put(sas_device);
 754        }
 755}
 756
 757/**
 758 * _scsih_sas_device_add - insert sas_device to the list.
 759 * @ioc: per adapter object
 760 * @sas_device: the sas_device object
 761 * Context: This function will acquire ioc->sas_device_lock.
 762 *
 763 * Adding new object to the ioc->sas_device_list.
 764 */
 765static void
 766_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
 767        struct _sas_device *sas_device)
 768{
 769        unsigned long flags;
 770
 771        dewtprintk(ioc, pr_info(MPT3SAS_FMT
 772                "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
 773                ioc->name, __func__, sas_device->handle,
 774                (unsigned long long)sas_device->sas_address));
 775
 776        if (sas_device->enclosure_handle != 0)
 777                dewtprintk(ioc, pr_info(MPT3SAS_FMT
 778                    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
 779                    ioc->name, __func__, (unsigned long long)
 780                    sas_device->enclosure_logical_id, sas_device->slot));
 781
 782        if (sas_device->connector_name[0] != '\0')
 783                dewtprintk(ioc, pr_info(MPT3SAS_FMT
 784                    "%s: enclosure level(0x%04x), connector name( %s)\n",
 785                    ioc->name, __func__,
 786                    sas_device->enclosure_level, sas_device->connector_name));
 787
 788        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 789        sas_device_get(sas_device);
 790        list_add_tail(&sas_device->list, &ioc->sas_device_list);
 791        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 792
 793        if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
 794             sas_device->sas_address_parent)) {
 795                _scsih_sas_device_remove(ioc, sas_device);
 796        } else if (!sas_device->starget) {
 797                /*
 798                 * When asyn scanning is enabled, its not possible to remove
 799                 * devices while scanning is turned on due to an oops in
 800                 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
 801                 */
 802                if (!ioc->is_driver_loading) {
 803                        mpt3sas_transport_port_remove(ioc,
 804                            sas_device->sas_address,
 805                            sas_device->sas_address_parent);
 806                        _scsih_sas_device_remove(ioc, sas_device);
 807                }
 808        }
 809}
 810
 811/**
 812 * _scsih_sas_device_init_add - insert sas_device to the list.
 813 * @ioc: per adapter object
 814 * @sas_device: the sas_device object
 815 * Context: This function will acquire ioc->sas_device_lock.
 816 *
 817 * Adding new object at driver load time to the ioc->sas_device_init_list.
 818 */
 819static void
 820_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
 821        struct _sas_device *sas_device)
 822{
 823        unsigned long flags;
 824
 825        dewtprintk(ioc, pr_info(MPT3SAS_FMT
 826                "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
 827                __func__, sas_device->handle,
 828                (unsigned long long)sas_device->sas_address));
 829
 830        if (sas_device->enclosure_handle != 0)
 831                dewtprintk(ioc, pr_info(MPT3SAS_FMT
 832                    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
 833                    ioc->name, __func__, (unsigned long long)
 834                    sas_device->enclosure_logical_id, sas_device->slot));
 835
 836        if (sas_device->connector_name[0] != '\0')
 837                dewtprintk(ioc, pr_info(MPT3SAS_FMT
 838                    "%s: enclosure level(0x%04x), connector name( %s)\n",
 839                    ioc->name, __func__, sas_device->enclosure_level,
 840                    sas_device->connector_name));
 841
 842        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 843        sas_device_get(sas_device);
 844        list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
 845        _scsih_determine_boot_device(ioc, sas_device, 0);
 846        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 847}
 848
 849/**
 850 * _scsih_raid_device_find_by_id - raid device search
 851 * @ioc: per adapter object
 852 * @id: sas device target id
 853 * @channel: sas device channel
 854 * Context: Calling function should acquire ioc->raid_device_lock
 855 *
 856 * This searches for raid_device based on target id, then return raid_device
 857 * object.
 858 */
 859static struct _raid_device *
 860_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
 861{
 862        struct _raid_device *raid_device, *r;
 863
 864        r = NULL;
 865        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 866                if (raid_device->id == id && raid_device->channel == channel) {
 867                        r = raid_device;
 868                        goto out;
 869                }
 870        }
 871
 872 out:
 873        return r;
 874}
 875
 876/**
 877 * mpt3sas_raid_device_find_by_handle - raid device search
 878 * @ioc: per adapter object
 879 * @handle: sas device handle (assigned by firmware)
 880 * Context: Calling function should acquire ioc->raid_device_lock
 881 *
 882 * This searches for raid_device based on handle, then return raid_device
 883 * object.
 884 */
 885struct _raid_device *
 886mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 887{
 888        struct _raid_device *raid_device, *r;
 889
 890        r = NULL;
 891        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 892                if (raid_device->handle != handle)
 893                        continue;
 894                r = raid_device;
 895                goto out;
 896        }
 897
 898 out:
 899        return r;
 900}
 901
 902/**
 903 * _scsih_raid_device_find_by_wwid - raid device search
 904 * @ioc: per adapter object
 905 * @handle: sas device handle (assigned by firmware)
 906 * Context: Calling function should acquire ioc->raid_device_lock
 907 *
 908 * This searches for raid_device based on wwid, then return raid_device
 909 * object.
 910 */
 911static struct _raid_device *
 912_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
 913{
 914        struct _raid_device *raid_device, *r;
 915
 916        r = NULL;
 917        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 918                if (raid_device->wwid != wwid)
 919                        continue;
 920                r = raid_device;
 921                goto out;
 922        }
 923
 924 out:
 925        return r;
 926}
 927
 928/**
 929 * _scsih_raid_device_add - add raid_device object
 930 * @ioc: per adapter object
 931 * @raid_device: raid_device object
 932 *
 933 * This is added to the raid_device_list link list.
 934 */
 935static void
 936_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
 937        struct _raid_device *raid_device)
 938{
 939        unsigned long flags;
 940
 941        dewtprintk(ioc, pr_info(MPT3SAS_FMT
 942                "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
 943            raid_device->handle, (unsigned long long)raid_device->wwid));
 944
 945        spin_lock_irqsave(&ioc->raid_device_lock, flags);
 946        list_add_tail(&raid_device->list, &ioc->raid_device_list);
 947        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
 948}
 949
 950/**
 951 * _scsih_raid_device_remove - delete raid_device object
 952 * @ioc: per adapter object
 953 * @raid_device: raid_device object
 954 *
 955 */
 956static void
 957_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
 958        struct _raid_device *raid_device)
 959{
 960        unsigned long flags;
 961
 962        spin_lock_irqsave(&ioc->raid_device_lock, flags);
 963        list_del(&raid_device->list);
 964        kfree(raid_device);
 965        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
 966}
 967
 968/**
 969 * mpt3sas_scsih_expander_find_by_handle - expander device search
 970 * @ioc: per adapter object
 971 * @handle: expander handle (assigned by firmware)
 972 * Context: Calling function should acquire ioc->sas_device_lock
 973 *
 974 * This searches for expander device based on handle, then returns the
 975 * sas_node object.
 976 */
 977struct _sas_node *
 978mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
 979{
 980        struct _sas_node *sas_expander, *r;
 981
 982        r = NULL;
 983        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
 984                if (sas_expander->handle != handle)
 985                        continue;
 986                r = sas_expander;
 987                goto out;
 988        }
 989 out:
 990        return r;
 991}
 992
 993/**
 994 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
 995 * @ioc: per adapter object
 996 * @sas_address: sas address
 997 * Context: Calling function should acquire ioc->sas_node_lock.
 998 *
 999 * This searches for expander device based on sas_address, then returns the
1000 * sas_node object.
1001 */
1002struct _sas_node *
1003mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1004        u64 sas_address)
1005{
1006        struct _sas_node *sas_expander, *r;
1007
1008        r = NULL;
1009        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1010                if (sas_expander->sas_address != sas_address)
1011                        continue;
1012                r = sas_expander;
1013                goto out;
1014        }
1015 out:
1016        return r;
1017}
1018
1019/**
1020 * _scsih_expander_node_add - insert expander device to the list.
1021 * @ioc: per adapter object
1022 * @sas_expander: the sas_device object
1023 * Context: This function will acquire ioc->sas_node_lock.
1024 *
1025 * Adding new object to the ioc->sas_expander_list.
1026 *
1027 * Return nothing.
1028 */
1029static void
1030_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1031        struct _sas_node *sas_expander)
1032{
1033        unsigned long flags;
1034
1035        spin_lock_irqsave(&ioc->sas_node_lock, flags);
1036        list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1037        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1038}
1039
1040/**
1041 * _scsih_is_end_device - determines if device is an end device
1042 * @device_info: bitfield providing information about the device.
1043 * Context: none
1044 *
1045 * Returns 1 if end device.
1046 */
1047static int
1048_scsih_is_end_device(u32 device_info)
1049{
1050        if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1051                ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1052                (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1053                (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1054                return 1;
1055        else
1056                return 0;
1057}
1058
1059/**
1060 * _scsih_scsi_lookup_get - returns scmd entry
1061 * @ioc: per adapter object
1062 * @smid: system request message index
1063 *
1064 * Returns the smid stored scmd pointer.
1065 */
1066static struct scsi_cmnd *
1067_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1068{
1069        return ioc->scsi_lookup[smid - 1].scmd;
1070}
1071
1072/**
1073 * _scsih_scsi_lookup_get_clear - returns scmd entry
1074 * @ioc: per adapter object
1075 * @smid: system request message index
1076 *
1077 * Returns the smid stored scmd pointer.
1078 * Then will derefrence the stored scmd pointer.
1079 */
1080static inline struct scsi_cmnd *
1081_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1082{
1083        unsigned long flags;
1084        struct scsi_cmnd *scmd;
1085
1086        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1087        scmd = ioc->scsi_lookup[smid - 1].scmd;
1088        ioc->scsi_lookup[smid - 1].scmd = NULL;
1089        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1090
1091        return scmd;
1092}
1093
1094/**
1095 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1096 * @ioc: per adapter object
1097 * @smid: system request message index
1098 * @scmd: pointer to scsi command object
1099 * Context: This function will acquire ioc->scsi_lookup_lock.
1100 *
1101 * This will search for a scmd pointer in the scsi_lookup array,
1102 * returning the revelent smid.  A returned value of zero means invalid.
1103 */
1104static u16
1105_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1106        *scmd)
1107{
1108        u16 smid;
1109        unsigned long   flags;
1110        int i;
1111
1112        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1113        smid = 0;
1114        for (i = 0; i < ioc->scsiio_depth; i++) {
1115                if (ioc->scsi_lookup[i].scmd == scmd) {
1116                        smid = ioc->scsi_lookup[i].smid;
1117                        goto out;
1118                }
1119        }
1120 out:
1121        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1122        return smid;
1123}
1124
1125/**
1126 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1127 * @ioc: per adapter object
1128 * @id: target id
1129 * @channel: channel
1130 * Context: This function will acquire ioc->scsi_lookup_lock.
1131 *
1132 * This will search for a matching channel:id in the scsi_lookup array,
1133 * returning 1 if found.
1134 */
1135static u8
1136_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1137        int channel)
1138{
1139        u8 found;
1140        unsigned long   flags;
1141        int i;
1142
1143        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1144        found = 0;
1145        for (i = 0 ; i < ioc->scsiio_depth; i++) {
1146                if (ioc->scsi_lookup[i].scmd &&
1147                    (ioc->scsi_lookup[i].scmd->device->id == id &&
1148                    ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1149                        found = 1;
1150                        goto out;
1151                }
1152        }
1153 out:
1154        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1155        return found;
1156}
1157
1158/**
1159 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1160 * @ioc: per adapter object
1161 * @id: target id
1162 * @lun: lun number
1163 * @channel: channel
1164 * Context: This function will acquire ioc->scsi_lookup_lock.
1165 *
1166 * This will search for a matching channel:id:lun in the scsi_lookup array,
1167 * returning 1 if found.
1168 */
1169static u8
1170_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1171        unsigned int lun, int channel)
1172{
1173        u8 found;
1174        unsigned long   flags;
1175        int i;
1176
1177        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1178        found = 0;
1179        for (i = 0 ; i < ioc->scsiio_depth; i++) {
1180                if (ioc->scsi_lookup[i].scmd &&
1181                    (ioc->scsi_lookup[i].scmd->device->id == id &&
1182                    ioc->scsi_lookup[i].scmd->device->channel == channel &&
1183                    ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1184                        found = 1;
1185                        goto out;
1186                }
1187        }
1188 out:
1189        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1190        return found;
1191}
1192
1193/**
1194 * scsih_change_queue_depth - setting device queue depth
1195 * @sdev: scsi device struct
1196 * @qdepth: requested queue depth
1197 *
1198 * Returns queue depth.
1199 */
1200int
1201scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1202{
1203        struct Scsi_Host *shost = sdev->host;
1204        int max_depth;
1205        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1206        struct MPT3SAS_DEVICE *sas_device_priv_data;
1207        struct MPT3SAS_TARGET *sas_target_priv_data;
1208        struct _sas_device *sas_device;
1209        unsigned long flags;
1210
1211        max_depth = shost->can_queue;
1212
1213        /* limit max device queue for SATA to 32 */
1214        sas_device_priv_data = sdev->hostdata;
1215        if (!sas_device_priv_data)
1216                goto not_sata;
1217        sas_target_priv_data = sas_device_priv_data->sas_target;
1218        if (!sas_target_priv_data)
1219                goto not_sata;
1220        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1221                goto not_sata;
1222
1223        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1224        sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1225        if (sas_device) {
1226                if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1227                        max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1228
1229                sas_device_put(sas_device);
1230        }
1231        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1232
1233 not_sata:
1234
1235        if (!sdev->tagged_supported)
1236                max_depth = 1;
1237        if (qdepth > max_depth)
1238                qdepth = max_depth;
1239        return scsi_change_queue_depth(sdev, qdepth);
1240}
1241
1242/**
1243 * scsih_target_alloc - target add routine
1244 * @starget: scsi target struct
1245 *
1246 * Returns 0 if ok. Any other return is assumed to be an error and
1247 * the device is ignored.
1248 */
1249int
1250scsih_target_alloc(struct scsi_target *starget)
1251{
1252        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1253        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1254        struct MPT3SAS_TARGET *sas_target_priv_data;
1255        struct _sas_device *sas_device;
1256        struct _raid_device *raid_device;
1257        unsigned long flags;
1258        struct sas_rphy *rphy;
1259
1260        sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1261                                       GFP_KERNEL);
1262        if (!sas_target_priv_data)
1263                return -ENOMEM;
1264
1265        starget->hostdata = sas_target_priv_data;
1266        sas_target_priv_data->starget = starget;
1267        sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1268
1269        /* RAID volumes */
1270        if (starget->channel == RAID_CHANNEL) {
1271                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1272                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1273                    starget->channel);
1274                if (raid_device) {
1275                        sas_target_priv_data->handle = raid_device->handle;
1276                        sas_target_priv_data->sas_address = raid_device->wwid;
1277                        sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1278                        sas_target_priv_data->raid_device = raid_device;
1279                        if (ioc->is_warpdrive)
1280                                raid_device->starget = starget;
1281                }
1282                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1283                return 0;
1284        }
1285
1286        /* sas/sata devices */
1287        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1288        rphy = dev_to_rphy(starget->dev.parent);
1289        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1290           rphy->identify.sas_address);
1291
1292        if (sas_device) {
1293                sas_target_priv_data->handle = sas_device->handle;
1294                sas_target_priv_data->sas_address = sas_device->sas_address;
1295                sas_target_priv_data->sdev = sas_device;
1296                sas_device->starget = starget;
1297                sas_device->id = starget->id;
1298                sas_device->channel = starget->channel;
1299                if (test_bit(sas_device->handle, ioc->pd_handles))
1300                        sas_target_priv_data->flags |=
1301                            MPT_TARGET_FLAGS_RAID_COMPONENT;
1302                if (sas_device->fast_path)
1303                        sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1304        }
1305        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1306
1307        return 0;
1308}
1309
1310/**
1311 * scsih_target_destroy - target destroy routine
1312 * @starget: scsi target struct
1313 *
1314 * Returns nothing.
1315 */
1316void
1317scsih_target_destroy(struct scsi_target *starget)
1318{
1319        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1320        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1321        struct MPT3SAS_TARGET *sas_target_priv_data;
1322        struct _sas_device *sas_device;
1323        struct _raid_device *raid_device;
1324        unsigned long flags;
1325        struct sas_rphy *rphy;
1326
1327        sas_target_priv_data = starget->hostdata;
1328        if (!sas_target_priv_data)
1329                return;
1330
1331        if (starget->channel == RAID_CHANNEL) {
1332                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1333                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1334                    starget->channel);
1335                if (raid_device) {
1336                        raid_device->starget = NULL;
1337                        raid_device->sdev = NULL;
1338                }
1339                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1340                goto out;
1341        }
1342
1343        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1344        rphy = dev_to_rphy(starget->dev.parent);
1345        sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1346        if (sas_device && (sas_device->starget == starget) &&
1347            (sas_device->id == starget->id) &&
1348            (sas_device->channel == starget->channel))
1349                sas_device->starget = NULL;
1350
1351        if (sas_device) {
1352                /*
1353                 * Corresponding get() is in _scsih_target_alloc()
1354                 */
1355                sas_target_priv_data->sdev = NULL;
1356                sas_device_put(sas_device);
1357
1358                sas_device_put(sas_device);
1359        }
1360        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1361
1362 out:
1363        kfree(sas_target_priv_data);
1364        starget->hostdata = NULL;
1365}
1366
1367/**
1368 * scsih_slave_alloc - device add routine
1369 * @sdev: scsi device struct
1370 *
1371 * Returns 0 if ok. Any other return is assumed to be an error and
1372 * the device is ignored.
1373 */
1374int
1375scsih_slave_alloc(struct scsi_device *sdev)
1376{
1377        struct Scsi_Host *shost;
1378        struct MPT3SAS_ADAPTER *ioc;
1379        struct MPT3SAS_TARGET *sas_target_priv_data;
1380        struct MPT3SAS_DEVICE *sas_device_priv_data;
1381        struct scsi_target *starget;
1382        struct _raid_device *raid_device;
1383        struct _sas_device *sas_device;
1384        unsigned long flags;
1385
1386        sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1387                                       GFP_KERNEL);
1388        if (!sas_device_priv_data)
1389                return -ENOMEM;
1390
1391        sas_device_priv_data->lun = sdev->lun;
1392        sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1393
1394        starget = scsi_target(sdev);
1395        sas_target_priv_data = starget->hostdata;
1396        sas_target_priv_data->num_luns++;
1397        sas_device_priv_data->sas_target = sas_target_priv_data;
1398        sdev->hostdata = sas_device_priv_data;
1399        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1400                sdev->no_uld_attach = 1;
1401
1402        shost = dev_to_shost(&starget->dev);
1403        ioc = shost_priv(shost);
1404        if (starget->channel == RAID_CHANNEL) {
1405                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1406                raid_device = _scsih_raid_device_find_by_id(ioc,
1407                    starget->id, starget->channel);
1408                if (raid_device)
1409                        raid_device->sdev = sdev; /* raid is single lun */
1410                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1411        }
1412
1413        if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1414                spin_lock_irqsave(&ioc->sas_device_lock, flags);
1415                sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1416                                        sas_target_priv_data->sas_address);
1417                if (sas_device && (sas_device->starget == NULL)) {
1418                        sdev_printk(KERN_INFO, sdev,
1419                        "%s : sas_device->starget set to starget @ %d\n",
1420                             __func__, __LINE__);
1421                        sas_device->starget = starget;
1422                }
1423
1424                if (sas_device)
1425                        sas_device_put(sas_device);
1426
1427                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1428        }
1429
1430        return 0;
1431}
1432
1433/**
1434 * scsih_slave_destroy - device destroy routine
1435 * @sdev: scsi device struct
1436 *
1437 * Returns nothing.
1438 */
1439void
1440scsih_slave_destroy(struct scsi_device *sdev)
1441{
1442        struct MPT3SAS_TARGET *sas_target_priv_data;
1443        struct scsi_target *starget;
1444        struct Scsi_Host *shost;
1445        struct MPT3SAS_ADAPTER *ioc;
1446        struct _sas_device *sas_device;
1447        unsigned long flags;
1448
1449        if (!sdev->hostdata)
1450                return;
1451
1452        starget = scsi_target(sdev);
1453        sas_target_priv_data = starget->hostdata;
1454        sas_target_priv_data->num_luns--;
1455
1456        shost = dev_to_shost(&starget->dev);
1457        ioc = shost_priv(shost);
1458
1459        if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1460                spin_lock_irqsave(&ioc->sas_device_lock, flags);
1461                sas_device = __mpt3sas_get_sdev_from_target(ioc,
1462                                sas_target_priv_data);
1463                if (sas_device && !sas_target_priv_data->num_luns)
1464                        sas_device->starget = NULL;
1465
1466                if (sas_device)
1467                        sas_device_put(sas_device);
1468                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1469        }
1470
1471        kfree(sdev->hostdata);
1472        sdev->hostdata = NULL;
1473}
1474
1475/**
1476 * _scsih_display_sata_capabilities - sata capabilities
1477 * @ioc: per adapter object
1478 * @handle: device handle
1479 * @sdev: scsi device struct
1480 */
1481static void
1482_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1483        u16 handle, struct scsi_device *sdev)
1484{
1485        Mpi2ConfigReply_t mpi_reply;
1486        Mpi2SasDevicePage0_t sas_device_pg0;
1487        u32 ioc_status;
1488        u16 flags;
1489        u32 device_info;
1490
1491        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1492            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1493                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1494                    ioc->name, __FILE__, __LINE__, __func__);
1495                return;
1496        }
1497
1498        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1499            MPI2_IOCSTATUS_MASK;
1500        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1501                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1502                    ioc->name, __FILE__, __LINE__, __func__);
1503                return;
1504        }
1505
1506        flags = le16_to_cpu(sas_device_pg0.Flags);
1507        device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1508
1509        sdev_printk(KERN_INFO, sdev,
1510            "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1511            "sw_preserve(%s)\n",
1512            (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1513            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1514            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1515            "n",
1516            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1517            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1518            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1519}
1520
1521/*
1522 * raid transport support -
1523 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1524 * unloading the driver followed by a load - I beleive that the subroutine
1525 * raid_class_release() is not cleaning up properly.
1526 */
1527
1528/**
1529 * scsih_is_raid - return boolean indicating device is raid volume
1530 * @dev the device struct object
1531 */
1532int
1533scsih_is_raid(struct device *dev)
1534{
1535        struct scsi_device *sdev = to_scsi_device(dev);
1536        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1537
1538        if (ioc->is_warpdrive)
1539                return 0;
1540        return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1541}
1542
1543/**
1544 * scsih_get_resync - get raid volume resync percent complete
1545 * @dev the device struct object
1546 */
1547void
1548scsih_get_resync(struct device *dev)
1549{
1550        struct scsi_device *sdev = to_scsi_device(dev);
1551        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1552        static struct _raid_device *raid_device;
1553        unsigned long flags;
1554        Mpi2RaidVolPage0_t vol_pg0;
1555        Mpi2ConfigReply_t mpi_reply;
1556        u32 volume_status_flags;
1557        u8 percent_complete;
1558        u16 handle;
1559
1560        percent_complete = 0;
1561        handle = 0;
1562        if (ioc->is_warpdrive)
1563                goto out;
1564
1565        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1566        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1567            sdev->channel);
1568        if (raid_device) {
1569                handle = raid_device->handle;
1570                percent_complete = raid_device->percent_complete;
1571        }
1572        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1573
1574        if (!handle)
1575                goto out;
1576
1577        if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1578             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1579             sizeof(Mpi2RaidVolPage0_t))) {
1580                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1581                    ioc->name, __FILE__, __LINE__, __func__);
1582                percent_complete = 0;
1583                goto out;
1584        }
1585
1586        volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1587        if (!(volume_status_flags &
1588            MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1589                percent_complete = 0;
1590
1591 out:
1592
1593        switch (ioc->hba_mpi_version_belonged) {
1594        case MPI2_VERSION:
1595                raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1596                break;
1597        case MPI25_VERSION:
1598        case MPI26_VERSION:
1599                raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1600                break;
1601        }
1602}
1603
1604/**
1605 * scsih_get_state - get raid volume level
1606 * @dev the device struct object
1607 */
1608void
1609scsih_get_state(struct device *dev)
1610{
1611        struct scsi_device *sdev = to_scsi_device(dev);
1612        struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1613        static struct _raid_device *raid_device;
1614        unsigned long flags;
1615        Mpi2RaidVolPage0_t vol_pg0;
1616        Mpi2ConfigReply_t mpi_reply;
1617        u32 volstate;
1618        enum raid_state state = RAID_STATE_UNKNOWN;
1619        u16 handle = 0;
1620
1621        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1622        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1623            sdev->channel);
1624        if (raid_device)
1625                handle = raid_device->handle;
1626        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1627
1628        if (!raid_device)
1629                goto out;
1630
1631        if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1632             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1633             sizeof(Mpi2RaidVolPage0_t))) {
1634                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1635                    ioc->name, __FILE__, __LINE__, __func__);
1636                goto out;
1637        }
1638
1639        volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1640        if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1641                state = RAID_STATE_RESYNCING;
1642                goto out;
1643        }
1644
1645        switch (vol_pg0.VolumeState) {
1646        case MPI2_RAID_VOL_STATE_OPTIMAL:
1647        case MPI2_RAID_VOL_STATE_ONLINE:
1648                state = RAID_STATE_ACTIVE;
1649                break;
1650        case  MPI2_RAID_VOL_STATE_DEGRADED:
1651                state = RAID_STATE_DEGRADED;
1652                break;
1653        case MPI2_RAID_VOL_STATE_FAILED:
1654        case MPI2_RAID_VOL_STATE_MISSING:
1655                state = RAID_STATE_OFFLINE;
1656                break;
1657        }
1658 out:
1659        switch (ioc->hba_mpi_version_belonged) {
1660        case MPI2_VERSION:
1661                raid_set_state(mpt2sas_raid_template, dev, state);
1662                break;
1663        case MPI25_VERSION:
1664        case MPI26_VERSION:
1665                raid_set_state(mpt3sas_raid_template, dev, state);
1666                break;
1667        }
1668}
1669
1670/**
1671 * _scsih_set_level - set raid level
1672 * @sdev: scsi device struct
1673 * @volume_type: volume type
1674 */
1675static void
1676_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1677        struct scsi_device *sdev, u8 volume_type)
1678{
1679        enum raid_level level = RAID_LEVEL_UNKNOWN;
1680
1681        switch (volume_type) {
1682        case MPI2_RAID_VOL_TYPE_RAID0:
1683                level = RAID_LEVEL_0;
1684                break;
1685        case MPI2_RAID_VOL_TYPE_RAID10:
1686                level = RAID_LEVEL_10;
1687                break;
1688        case MPI2_RAID_VOL_TYPE_RAID1E:
1689                level = RAID_LEVEL_1E;
1690                break;
1691        case MPI2_RAID_VOL_TYPE_RAID1:
1692                level = RAID_LEVEL_1;
1693                break;
1694        }
1695
1696        switch (ioc->hba_mpi_version_belonged) {
1697        case MPI2_VERSION:
1698                raid_set_level(mpt2sas_raid_template,
1699                        &sdev->sdev_gendev, level);
1700                break;
1701        case MPI25_VERSION:
1702        case MPI26_VERSION:
1703                raid_set_level(mpt3sas_raid_template,
1704                        &sdev->sdev_gendev, level);
1705                break;
1706        }
1707}
1708
1709
1710/**
1711 * _scsih_get_volume_capabilities - volume capabilities
1712 * @ioc: per adapter object
1713 * @sas_device: the raid_device object
1714 *
1715 * Returns 0 for success, else 1
1716 */
1717static int
1718_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1719        struct _raid_device *raid_device)
1720{
1721        Mpi2RaidVolPage0_t *vol_pg0;
1722        Mpi2RaidPhysDiskPage0_t pd_pg0;
1723        Mpi2SasDevicePage0_t sas_device_pg0;
1724        Mpi2ConfigReply_t mpi_reply;
1725        u16 sz;
1726        u8 num_pds;
1727
1728        if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1729            &num_pds)) || !num_pds) {
1730                dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1731                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1732                    __func__));
1733                return 1;
1734        }
1735
1736        raid_device->num_pds = num_pds;
1737        sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1738            sizeof(Mpi2RaidVol0PhysDisk_t));
1739        vol_pg0 = kzalloc(sz, GFP_KERNEL);
1740        if (!vol_pg0) {
1741                dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1742                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1743                    __func__));
1744                return 1;
1745        }
1746
1747        if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1748             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1749                dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1750                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1751                    __func__));
1752                kfree(vol_pg0);
1753                return 1;
1754        }
1755
1756        raid_device->volume_type = vol_pg0->VolumeType;
1757
1758        /* figure out what the underlying devices are by
1759         * obtaining the device_info bits for the 1st device
1760         */
1761        if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1762            &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1763            vol_pg0->PhysDisk[0].PhysDiskNum))) {
1764                if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1765                    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1766                    le16_to_cpu(pd_pg0.DevHandle)))) {
1767                        raid_device->device_info =
1768                            le32_to_cpu(sas_device_pg0.DeviceInfo);
1769                }
1770        }
1771
1772        kfree(vol_pg0);
1773        return 0;
1774}
1775
1776/**
1777 * _scsih_enable_tlr - setting TLR flags
1778 * @ioc: per adapter object
1779 * @sdev: scsi device struct
1780 *
1781 * Enabling Transaction Layer Retries for tape devices when
1782 * vpd page 0x90 is present
1783 *
1784 */
1785static void
1786_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1787{
1788
1789        /* only for TAPE */
1790        if (sdev->type != TYPE_TAPE)
1791                return;
1792
1793        if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1794                return;
1795
1796        sas_enable_tlr(sdev);
1797        sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1798            sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1799        return;
1800
1801}
1802
1803/**
1804 * scsih_slave_configure - device configure routine.
1805 * @sdev: scsi device struct
1806 *
1807 * Returns 0 if ok. Any other return is assumed to be an error and
1808 * the device is ignored.
1809 */
1810int
1811scsih_slave_configure(struct scsi_device *sdev)
1812{
1813        struct Scsi_Host *shost = sdev->host;
1814        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1815        struct MPT3SAS_DEVICE *sas_device_priv_data;
1816        struct MPT3SAS_TARGET *sas_target_priv_data;
1817        struct _sas_device *sas_device;
1818        struct _raid_device *raid_device;
1819        unsigned long flags;
1820        int qdepth;
1821        u8 ssp_target = 0;
1822        char *ds = "";
1823        char *r_level = "";
1824        u16 handle, volume_handle = 0;
1825        u64 volume_wwid = 0;
1826
1827        qdepth = 1;
1828        sas_device_priv_data = sdev->hostdata;
1829        sas_device_priv_data->configured_lun = 1;
1830        sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1831        sas_target_priv_data = sas_device_priv_data->sas_target;
1832        handle = sas_target_priv_data->handle;
1833
1834        /* raid volume handling */
1835        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1836
1837                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1838                raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
1839                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1840                if (!raid_device) {
1841                        dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1842                            "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1843                            __LINE__, __func__));
1844                        return 1;
1845                }
1846
1847                if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1848                        dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1849                            "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1850                            __LINE__, __func__));
1851                        return 1;
1852                }
1853
1854                /*
1855                 * WARPDRIVE: Initialize the required data for Direct IO
1856                 */
1857                mpt3sas_init_warpdrive_properties(ioc, raid_device);
1858
1859                /* RAID Queue Depth Support
1860                 * IS volume = underlying qdepth of drive type, either
1861                 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1862                 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1863                 */
1864                if (raid_device->device_info &
1865                    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1866                        qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1867                        ds = "SSP";
1868                } else {
1869                        qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1870                         if (raid_device->device_info &
1871                            MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1872                                ds = "SATA";
1873                        else
1874                                ds = "STP";
1875                }
1876
1877                switch (raid_device->volume_type) {
1878                case MPI2_RAID_VOL_TYPE_RAID0:
1879                        r_level = "RAID0";
1880                        break;
1881                case MPI2_RAID_VOL_TYPE_RAID1E:
1882                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1883                        if (ioc->manu_pg10.OEMIdentifier &&
1884                            (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1885                            MFG10_GF0_R10_DISPLAY) &&
1886                            !(raid_device->num_pds % 2))
1887                                r_level = "RAID10";
1888                        else
1889                                r_level = "RAID1E";
1890                        break;
1891                case MPI2_RAID_VOL_TYPE_RAID1:
1892                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1893                        r_level = "RAID1";
1894                        break;
1895                case MPI2_RAID_VOL_TYPE_RAID10:
1896                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1897                        r_level = "RAID10";
1898                        break;
1899                case MPI2_RAID_VOL_TYPE_UNKNOWN:
1900                default:
1901                        qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1902                        r_level = "RAIDX";
1903                        break;
1904                }
1905
1906                if (!ioc->hide_ir_msg)
1907                        sdev_printk(KERN_INFO, sdev,
1908                           "%s: handle(0x%04x), wwid(0x%016llx),"
1909                            " pd_count(%d), type(%s)\n",
1910                            r_level, raid_device->handle,
1911                            (unsigned long long)raid_device->wwid,
1912                            raid_device->num_pds, ds);
1913
1914                scsih_change_queue_depth(sdev, qdepth);
1915
1916                /* raid transport support */
1917                if (!ioc->is_warpdrive)
1918                        _scsih_set_level(ioc, sdev, raid_device->volume_type);
1919                return 0;
1920        }
1921
1922        /* non-raid handling */
1923        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1924                if (mpt3sas_config_get_volume_handle(ioc, handle,
1925                    &volume_handle)) {
1926                        dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1927                            "failure at %s:%d/%s()!\n", ioc->name,
1928                            __FILE__, __LINE__, __func__));
1929                        return 1;
1930                }
1931                if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1932                    volume_handle, &volume_wwid)) {
1933                        dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1934                            "failure at %s:%d/%s()!\n", ioc->name,
1935                            __FILE__, __LINE__, __func__));
1936                        return 1;
1937                }
1938        }
1939
1940        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1941        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1942           sas_device_priv_data->sas_target->sas_address);
1943        if (!sas_device) {
1944                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1945                dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1946                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1947                    __func__));
1948                return 1;
1949        }
1950
1951        sas_device->volume_handle = volume_handle;
1952        sas_device->volume_wwid = volume_wwid;
1953        if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1954                qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1955                ssp_target = 1;
1956                if (sas_device->device_info &
1957                                MPI2_SAS_DEVICE_INFO_SEP) {
1958                        sdev_printk(KERN_WARNING, sdev,
1959                        "set ignore_delay_remove for handle(0x%04x)\n",
1960                        sas_device_priv_data->sas_target->handle);
1961                        sas_device_priv_data->ignore_delay_remove = 1;
1962                        ds = "SES";
1963                } else
1964                        ds = "SSP";
1965        } else {
1966                qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1967                if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1968                        ds = "STP";
1969                else if (sas_device->device_info &
1970                    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1971                        ds = "SATA";
1972        }
1973
1974        sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1975            "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1976            ds, handle, (unsigned long long)sas_device->sas_address,
1977            sas_device->phy, (unsigned long long)sas_device->device_name);
1978        if (sas_device->enclosure_handle != 0)
1979                sdev_printk(KERN_INFO, sdev,
1980                     "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1981                     ds, (unsigned long long)
1982                     sas_device->enclosure_logical_id, sas_device->slot);
1983        if (sas_device->connector_name[0] != '\0')
1984                sdev_printk(KERN_INFO, sdev,
1985                     "%s: enclosure level(0x%04x), connector name( %s)\n",
1986                     ds, sas_device->enclosure_level,
1987                     sas_device->connector_name);
1988
1989        sas_device_put(sas_device);
1990        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1991
1992        if (!ssp_target)
1993                _scsih_display_sata_capabilities(ioc, handle, sdev);
1994
1995
1996        scsih_change_queue_depth(sdev, qdepth);
1997
1998        if (ssp_target) {
1999                sas_read_port_mode_page(sdev);
2000                _scsih_enable_tlr(ioc, sdev);
2001        }
2002
2003        return 0;
2004}
2005
2006/**
2007 * scsih_bios_param - fetch head, sector, cylinder info for a disk
2008 * @sdev: scsi device struct
2009 * @bdev: pointer to block device context
2010 * @capacity: device size (in 512 byte sectors)
2011 * @params: three element array to place output:
2012 *              params[0] number of heads (max 255)
2013 *              params[1] number of sectors (max 63)
2014 *              params[2] number of cylinders
2015 *
2016 * Return nothing.
2017 */
2018int
2019scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2020        sector_t capacity, int params[])
2021{
2022        int             heads;
2023        int             sectors;
2024        sector_t        cylinders;
2025        ulong           dummy;
2026
2027        heads = 64;
2028        sectors = 32;
2029
2030        dummy = heads * sectors;
2031        cylinders = capacity;
2032        sector_div(cylinders, dummy);
2033
2034        /*
2035         * Handle extended translation size for logical drives
2036         * > 1Gb
2037         */
2038        if ((ulong)capacity >= 0x200000) {
2039                heads = 255;
2040                sectors = 63;
2041                dummy = heads * sectors;
2042                cylinders = capacity;
2043                sector_div(cylinders, dummy);
2044        }
2045
2046        /* return result */
2047        params[0] = heads;
2048        params[1] = sectors;
2049        params[2] = cylinders;
2050
2051        return 0;
2052}
2053
2054/**
2055 * _scsih_response_code - translation of device response code
2056 * @ioc: per adapter object
2057 * @response_code: response code returned by the device
2058 *
2059 * Return nothing.
2060 */
2061static void
2062_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2063{
2064        char *desc;
2065
2066        switch (response_code) {
2067        case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2068                desc = "task management request completed";
2069                break;
2070        case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2071                desc = "invalid frame";
2072                break;
2073        case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2074                desc = "task management request not supported";
2075                break;
2076        case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2077                desc = "task management request failed";
2078                break;
2079        case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2080                desc = "task management request succeeded";
2081                break;
2082        case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2083                desc = "invalid lun";
2084                break;
2085        case 0xA:
2086                desc = "overlapped tag attempted";
2087                break;
2088        case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2089                desc = "task queued, however not sent to target";
2090                break;
2091        default:
2092                desc = "unknown";
2093                break;
2094        }
2095        pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2096                ioc->name, response_code, desc);
2097}
2098
2099/**
2100 * _scsih_tm_done - tm completion routine
2101 * @ioc: per adapter object
2102 * @smid: system request message index
2103 * @msix_index: MSIX table index supplied by the OS
2104 * @reply: reply message frame(lower 32bit addr)
2105 * Context: none.
2106 *
2107 * The callback handler when using scsih_issue_tm.
2108 *
2109 * Return 1 meaning mf should be freed from _base_interrupt
2110 *        0 means the mf is freed from this function.
2111 */
2112static u8
2113_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2114{
2115        MPI2DefaultReply_t *mpi_reply;
2116
2117        if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2118                return 1;
2119        if (ioc->tm_cmds.smid != smid)
2120                return 1;
2121        mpt3sas_base_flush_reply_queues(ioc);
2122        ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2123        mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2124        if (mpi_reply) {
2125                memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2126                ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2127        }
2128        ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2129        complete(&ioc->tm_cmds.done);
2130        return 1;
2131}
2132
2133/**
2134 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2135 * @ioc: per adapter object
2136 * @handle: device handle
2137 *
2138 * During taskmangement request, we need to freeze the device queue.
2139 */
2140void
2141mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2142{
2143        struct MPT3SAS_DEVICE *sas_device_priv_data;
2144        struct scsi_device *sdev;
2145        u8 skip = 0;
2146
2147        shost_for_each_device(sdev, ioc->shost) {
2148                if (skip)
2149                        continue;
2150                sas_device_priv_data = sdev->hostdata;
2151                if (!sas_device_priv_data)
2152                        continue;
2153                if (sas_device_priv_data->sas_target->handle == handle) {
2154                        sas_device_priv_data->sas_target->tm_busy = 1;
2155                        skip = 1;
2156                        ioc->ignore_loginfos = 1;
2157                }
2158        }
2159}
2160
2161/**
2162 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2163 * @ioc: per adapter object
2164 * @handle: device handle
2165 *
2166 * During taskmangement request, we need to freeze the device queue.
2167 */
2168void
2169mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2170{
2171        struct MPT3SAS_DEVICE *sas_device_priv_data;
2172        struct scsi_device *sdev;
2173        u8 skip = 0;
2174
2175        shost_for_each_device(sdev, ioc->shost) {
2176                if (skip)
2177                        continue;
2178                sas_device_priv_data = sdev->hostdata;
2179                if (!sas_device_priv_data)
2180                        continue;
2181                if (sas_device_priv_data->sas_target->handle == handle) {
2182                        sas_device_priv_data->sas_target->tm_busy = 0;
2183                        skip = 1;
2184                        ioc->ignore_loginfos = 0;
2185                }
2186        }
2187}
2188
2189/**
2190 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2191 * @ioc: per adapter struct
2192 * @device_handle: device handle
2193 * @channel: the channel assigned by the OS
2194 * @id: the id assigned by the OS
2195 * @lun: lun number
2196 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2197 * @smid_task: smid assigned to the task
2198 * @timeout: timeout in seconds
2199 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2200 * Context: user
2201 *
2202 * A generic API for sending task management requests to firmware.
2203 *
2204 * The callback index is set inside `ioc->tm_cb_idx`.
2205 *
2206 * Return SUCCESS or FAILED.
2207 */
2208int
2209mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2210        uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2211        enum mutex_type m_type)
2212{
2213        Mpi2SCSITaskManagementRequest_t *mpi_request;
2214        Mpi2SCSITaskManagementReply_t *mpi_reply;
2215        u16 smid = 0;
2216        u32 ioc_state;
2217        unsigned long timeleft;
2218        struct scsiio_tracker *scsi_lookup = NULL;
2219        int rc;
2220        u16 msix_task = 0;
2221
2222        if (m_type == TM_MUTEX_ON)
2223                mutex_lock(&ioc->tm_cmds.mutex);
2224        if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2225                pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2226                    __func__, ioc->name);
2227                rc = FAILED;
2228                goto err_out;
2229        }
2230
2231        if (ioc->shost_recovery || ioc->remove_host ||
2232            ioc->pci_error_recovery) {
2233                pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2234                    __func__, ioc->name);
2235                rc = FAILED;
2236                goto err_out;
2237        }
2238
2239        ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2240        if (ioc_state & MPI2_DOORBELL_USED) {
2241                dhsprintk(ioc, pr_info(MPT3SAS_FMT
2242                        "unexpected doorbell active!\n", ioc->name));
2243                rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2244                    FORCE_BIG_HAMMER);
2245                rc = (!rc) ? SUCCESS : FAILED;
2246                goto err_out;
2247        }
2248
2249        if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2250                mpt3sas_base_fault_info(ioc, ioc_state &
2251                    MPI2_DOORBELL_DATA_MASK);
2252                rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2253                    FORCE_BIG_HAMMER);
2254                rc = (!rc) ? SUCCESS : FAILED;
2255                goto err_out;
2256        }
2257
2258        smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2259        if (!smid) {
2260                pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2261                    ioc->name, __func__);
2262                rc = FAILED;
2263                goto err_out;
2264        }
2265
2266        if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2267                scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2268
2269        dtmprintk(ioc, pr_info(MPT3SAS_FMT
2270                "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2271                ioc->name, handle, type, smid_task));
2272        ioc->tm_cmds.status = MPT3_CMD_PENDING;
2273        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2274        ioc->tm_cmds.smid = smid;
2275        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2276        memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2277        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2278        mpi_request->DevHandle = cpu_to_le16(handle);
2279        mpi_request->TaskType = type;
2280        mpi_request->TaskMID = cpu_to_le16(smid_task);
2281        int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2282        mpt3sas_scsih_set_tm_flag(ioc, handle);
2283        init_completion(&ioc->tm_cmds.done);
2284        if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2285                        (scsi_lookup->msix_io < ioc->reply_queue_count))
2286                msix_task = scsi_lookup->msix_io;
2287        else
2288                msix_task = 0;
2289        mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
2290        timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2291        if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2292                pr_err(MPT3SAS_FMT "%s: timeout\n",
2293                    ioc->name, __func__);
2294                _debug_dump_mf(mpi_request,
2295                    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2296                if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2297                        rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2298                            FORCE_BIG_HAMMER);
2299                        rc = (!rc) ? SUCCESS : FAILED;
2300                        ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2301                        mpt3sas_scsih_clear_tm_flag(ioc, handle);
2302                        goto err_out;
2303                }
2304        }
2305
2306        if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2307                mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2308                mpi_reply = ioc->tm_cmds.reply;
2309                dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2310                    "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2311                    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2312                    le32_to_cpu(mpi_reply->IOCLogInfo),
2313                    le32_to_cpu(mpi_reply->TerminationCount)));
2314                if (ioc->logging_level & MPT_DEBUG_TM) {
2315                        _scsih_response_code(ioc, mpi_reply->ResponseCode);
2316                        if (mpi_reply->IOCStatus)
2317                                _debug_dump_mf(mpi_request,
2318                                    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2319                }
2320        }
2321
2322        switch (type) {
2323        case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2324                rc = SUCCESS;
2325                if (scsi_lookup->scmd == NULL)
2326                        break;
2327                rc = FAILED;
2328                break;
2329
2330        case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2331                if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2332                        rc = FAILED;
2333                else
2334                        rc = SUCCESS;
2335                break;
2336        case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2337        case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2338                if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2339                        rc = FAILED;
2340                else
2341                        rc = SUCCESS;
2342                break;
2343        case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2344                rc = SUCCESS;
2345                break;
2346        default:
2347                rc = FAILED;
2348                break;
2349        }
2350
2351        mpt3sas_scsih_clear_tm_flag(ioc, handle);
2352        ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2353        if (m_type == TM_MUTEX_ON)
2354                mutex_unlock(&ioc->tm_cmds.mutex);
2355
2356        return rc;
2357
2358 err_out:
2359        if (m_type == TM_MUTEX_ON)
2360                mutex_unlock(&ioc->tm_cmds.mutex);
2361        return rc;
2362}
2363
2364/**
2365 * _scsih_tm_display_info - displays info about the device
2366 * @ioc: per adapter struct
2367 * @scmd: pointer to scsi command object
2368 *
2369 * Called by task management callback handlers.
2370 */
2371static void
2372_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2373{
2374        struct scsi_target *starget = scmd->device->sdev_target;
2375        struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2376        struct _sas_device *sas_device = NULL;
2377        unsigned long flags;
2378        char *device_str = NULL;
2379
2380        if (!priv_target)
2381                return;
2382        if (ioc->hide_ir_msg)
2383                device_str = "WarpDrive";
2384        else
2385                device_str = "volume";
2386
2387        scsi_print_command(scmd);
2388        if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2389                starget_printk(KERN_INFO, starget,
2390                        "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2391                        device_str, priv_target->handle,
2392                    device_str, (unsigned long long)priv_target->sas_address);
2393        } else {
2394                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2395                sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2396                if (sas_device) {
2397                        if (priv_target->flags &
2398                            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2399                                starget_printk(KERN_INFO, starget,
2400                                    "volume handle(0x%04x), "
2401                                    "volume wwid(0x%016llx)\n",
2402                                    sas_device->volume_handle,
2403                                   (unsigned long long)sas_device->volume_wwid);
2404                        }
2405                        starget_printk(KERN_INFO, starget,
2406                            "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2407                            sas_device->handle,
2408                            (unsigned long long)sas_device->sas_address,
2409                            sas_device->phy);
2410                        if (sas_device->enclosure_handle != 0)
2411                                starget_printk(KERN_INFO, starget,
2412                                 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2413                                 (unsigned long long)
2414                                 sas_device->enclosure_logical_id,
2415                                 sas_device->slot);
2416                        if (sas_device->connector_name[0] != '\0')
2417                                starget_printk(KERN_INFO, starget,
2418                                "enclosure level(0x%04x),connector name(%s)\n",
2419                                 sas_device->enclosure_level,
2420                                 sas_device->connector_name);
2421
2422                        sas_device_put(sas_device);
2423                }
2424                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2425        }
2426}
2427
2428/**
2429 * scsih_abort - eh threads main abort routine
2430 * @scmd: pointer to scsi command object
2431 *
2432 * Returns SUCCESS if command aborted else FAILED
2433 */
2434int
2435scsih_abort(struct scsi_cmnd *scmd)
2436{
2437        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2438        struct MPT3SAS_DEVICE *sas_device_priv_data;
2439        u16 smid;
2440        u16 handle;
2441        int r;
2442
2443        sdev_printk(KERN_INFO, scmd->device,
2444                "attempting task abort! scmd(%p)\n", scmd);
2445        _scsih_tm_display_info(ioc, scmd);
2446
2447        sas_device_priv_data = scmd->device->hostdata;
2448        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2449                sdev_printk(KERN_INFO, scmd->device,
2450                        "device been deleted! scmd(%p)\n", scmd);
2451                scmd->result = DID_NO_CONNECT << 16;
2452                scmd->scsi_done(scmd);
2453                r = SUCCESS;
2454                goto out;
2455        }
2456
2457        /* search for the command */
2458        smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2459        if (!smid) {
2460                scmd->result = DID_RESET << 16;
2461                r = SUCCESS;
2462                goto out;
2463        }
2464
2465        /* for hidden raid components and volumes this is not supported */
2466        if (sas_device_priv_data->sas_target->flags &
2467            MPT_TARGET_FLAGS_RAID_COMPONENT ||
2468            sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2469                scmd->result = DID_RESET << 16;
2470                r = FAILED;
2471                goto out;
2472        }
2473
2474        mpt3sas_halt_firmware(ioc);
2475
2476        handle = sas_device_priv_data->sas_target->handle;
2477        r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2478            scmd->device->id, scmd->device->lun,
2479            MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2480
2481 out:
2482        sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2483            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2484        return r;
2485}
2486
2487/**
2488 * scsih_dev_reset - eh threads main device reset routine
2489 * @scmd: pointer to scsi command object
2490 *
2491 * Returns SUCCESS if command aborted else FAILED
2492 */
2493int
2494scsih_dev_reset(struct scsi_cmnd *scmd)
2495{
2496        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2497        struct MPT3SAS_DEVICE *sas_device_priv_data;
2498        struct _sas_device *sas_device = NULL;
2499        u16     handle;
2500        int r;
2501
2502        struct scsi_target *starget = scmd->device->sdev_target;
2503        struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2504
2505        sdev_printk(KERN_INFO, scmd->device,
2506                "attempting device reset! scmd(%p)\n", scmd);
2507        _scsih_tm_display_info(ioc, scmd);
2508
2509        sas_device_priv_data = scmd->device->hostdata;
2510        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2511                sdev_printk(KERN_INFO, scmd->device,
2512                        "device been deleted! scmd(%p)\n", scmd);
2513                scmd->result = DID_NO_CONNECT << 16;
2514                scmd->scsi_done(scmd);
2515                r = SUCCESS;
2516                goto out;
2517        }
2518
2519        /* for hidden raid components obtain the volume_handle */
2520        handle = 0;
2521        if (sas_device_priv_data->sas_target->flags &
2522            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2523                sas_device = mpt3sas_get_sdev_from_target(ioc,
2524                                target_priv_data);
2525                if (sas_device)
2526                        handle = sas_device->volume_handle;
2527        } else
2528                handle = sas_device_priv_data->sas_target->handle;
2529
2530        if (!handle) {
2531                scmd->result = DID_RESET << 16;
2532                r = FAILED;
2533                goto out;
2534        }
2535
2536        r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2537            scmd->device->id, scmd->device->lun,
2538            MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2539
2540 out:
2541        sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2542            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2543
2544        if (sas_device)
2545                sas_device_put(sas_device);
2546
2547        return r;
2548}
2549
2550/**
2551 * scsih_target_reset - eh threads main target reset routine
2552 * @scmd: pointer to scsi command object
2553 *
2554 * Returns SUCCESS if command aborted else FAILED
2555 */
2556int
2557scsih_target_reset(struct scsi_cmnd *scmd)
2558{
2559        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2560        struct MPT3SAS_DEVICE *sas_device_priv_data;
2561        struct _sas_device *sas_device = NULL;
2562        u16     handle;
2563        int r;
2564        struct scsi_target *starget = scmd->device->sdev_target;
2565        struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2566
2567        starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2568                scmd);
2569        _scsih_tm_display_info(ioc, scmd);
2570
2571        sas_device_priv_data = scmd->device->hostdata;
2572        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2573                starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2574                        scmd);
2575                scmd->result = DID_NO_CONNECT << 16;
2576                scmd->scsi_done(scmd);
2577                r = SUCCESS;
2578                goto out;
2579        }
2580
2581        /* for hidden raid components obtain the volume_handle */
2582        handle = 0;
2583        if (sas_device_priv_data->sas_target->flags &
2584            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2585                sas_device = mpt3sas_get_sdev_from_target(ioc,
2586                                target_priv_data);
2587                if (sas_device)
2588                        handle = sas_device->volume_handle;
2589        } else
2590                handle = sas_device_priv_data->sas_target->handle;
2591
2592        if (!handle) {
2593                scmd->result = DID_RESET << 16;
2594                r = FAILED;
2595                goto out;
2596        }
2597
2598        r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2599            scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2600            30, TM_MUTEX_ON);
2601
2602 out:
2603        starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2604            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2605
2606        if (sas_device)
2607                sas_device_put(sas_device);
2608
2609        return r;
2610}
2611
2612
2613/**
2614 * scsih_host_reset - eh threads main host reset routine
2615 * @scmd: pointer to scsi command object
2616 *
2617 * Returns SUCCESS if command aborted else FAILED
2618 */
2619int
2620scsih_host_reset(struct scsi_cmnd *scmd)
2621{
2622        struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2623        int r, retval;
2624
2625        pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2626            ioc->name, scmd);
2627        scsi_print_command(scmd);
2628
2629        if (ioc->is_driver_loading) {
2630                pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2631                    ioc->name);
2632                r = FAILED;
2633                goto out;
2634        }
2635
2636        retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2637            FORCE_BIG_HAMMER);
2638        r = (retval < 0) ? FAILED : SUCCESS;
2639out:
2640        pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2641            ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2642
2643        return r;
2644}
2645
2646/**
2647 * _scsih_fw_event_add - insert and queue up fw_event
2648 * @ioc: per adapter object
2649 * @fw_event: object describing the event
2650 * Context: This function will acquire ioc->fw_event_lock.
2651 *
2652 * This adds the firmware event object into link list, then queues it up to
2653 * be processed from user context.
2654 *
2655 * Return nothing.
2656 */
2657static void
2658_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2659{
2660        unsigned long flags;
2661
2662        if (ioc->firmware_event_thread == NULL)
2663                return;
2664
2665        spin_lock_irqsave(&ioc->fw_event_lock, flags);
2666        fw_event_work_get(fw_event);
2667        INIT_LIST_HEAD(&fw_event->list);
2668        list_add_tail(&fw_event->list, &ioc->fw_event_list);
2669        INIT_WORK(&fw_event->work, _firmware_event_work);
2670        fw_event_work_get(fw_event);
2671        queue_work(ioc->firmware_event_thread, &fw_event->work);
2672        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2673}
2674
2675/**
2676 * _scsih_fw_event_del_from_list - delete fw_event from the list
2677 * @ioc: per adapter object
2678 * @fw_event: object describing the event
2679 * Context: This function will acquire ioc->fw_event_lock.
2680 *
2681 * If the fw_event is on the fw_event_list, remove it and do a put.
2682 *
2683 * Return nothing.
2684 */
2685static void
2686_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2687        *fw_event)
2688{
2689        unsigned long flags;
2690
2691        spin_lock_irqsave(&ioc->fw_event_lock, flags);
2692        if (!list_empty(&fw_event->list)) {
2693                list_del_init(&fw_event->list);
2694                fw_event_work_put(fw_event);
2695        }
2696        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2697}
2698
2699
2700 /**
2701 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2702 * @ioc: per adapter object
2703 * @event_data: trigger event data
2704 *
2705 * Return nothing.
2706 */
2707void
2708mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2709        struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2710{
2711        struct fw_event_work *fw_event;
2712        u16 sz;
2713
2714        if (ioc->is_driver_loading)
2715                return;
2716        sz = sizeof(*event_data);
2717        fw_event = alloc_fw_event_work(sz);
2718        if (!fw_event)
2719                return;
2720        fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2721        fw_event->ioc = ioc;
2722        memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2723        _scsih_fw_event_add(ioc, fw_event);
2724        fw_event_work_put(fw_event);
2725}
2726
2727/**
2728 * _scsih_error_recovery_delete_devices - remove devices not responding
2729 * @ioc: per adapter object
2730 *
2731 * Return nothing.
2732 */
2733static void
2734_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2735{
2736        struct fw_event_work *fw_event;
2737
2738        if (ioc->is_driver_loading)
2739                return;
2740        fw_event = alloc_fw_event_work(0);
2741        if (!fw_event)
2742                return;
2743        fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2744        fw_event->ioc = ioc;
2745        _scsih_fw_event_add(ioc, fw_event);
2746        fw_event_work_put(fw_event);
2747}
2748
2749/**
2750 * mpt3sas_port_enable_complete - port enable completed (fake event)
2751 * @ioc: per adapter object
2752 *
2753 * Return nothing.
2754 */
2755void
2756mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2757{
2758        struct fw_event_work *fw_event;
2759
2760        fw_event = alloc_fw_event_work(0);
2761        if (!fw_event)
2762                return;
2763        fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2764        fw_event->ioc = ioc;
2765        _scsih_fw_event_add(ioc, fw_event);
2766        fw_event_work_put(fw_event);
2767}
2768
2769static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2770{
2771        unsigned long flags;
2772        struct fw_event_work *fw_event = NULL;
2773
2774        spin_lock_irqsave(&ioc->fw_event_lock, flags);
2775        if (!list_empty(&ioc->fw_event_list)) {
2776                fw_event = list_first_entry(&ioc->fw_event_list,
2777                                struct fw_event_work, list);
2778                list_del_init(&fw_event->list);
2779        }
2780        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2781
2782        return fw_event;
2783}
2784
2785/**
2786 * _scsih_fw_event_cleanup_queue - cleanup event queue
2787 * @ioc: per adapter object
2788 *
2789 * Walk the firmware event queue, either killing timers, or waiting
2790 * for outstanding events to complete
2791 *
2792 * Return nothing.
2793 */
2794static void
2795_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2796{
2797        struct fw_event_work *fw_event;
2798
2799        if (list_empty(&ioc->fw_event_list) ||
2800             !ioc->firmware_event_thread || in_interrupt())
2801                return;
2802
2803        while ((fw_event = dequeue_next_fw_event(ioc))) {
2804                /*
2805                 * Wait on the fw_event to complete. If this returns 1, then
2806                 * the event was never executed, and we need a put for the
2807                 * reference the delayed_work had on the fw_event.
2808                 *
2809                 * If it did execute, we wait for it to finish, and the put will
2810                 * happen from _firmware_event_work()
2811                 */
2812                if (cancel_delayed_work_sync(&fw_event->delayed_work))
2813                        fw_event_work_put(fw_event);
2814
2815                fw_event_work_put(fw_event);
2816        }
2817}
2818
2819/**
2820 * _scsih_internal_device_block - block the sdev device
2821 * @sdev: per device object
2822 * @sas_device_priv_data : per device driver private data
2823 *
2824 * make sure device is blocked without error, if not
2825 * print an error
2826 */
2827static void
2828_scsih_internal_device_block(struct scsi_device *sdev,
2829                        struct MPT3SAS_DEVICE *sas_device_priv_data)
2830{
2831        int r = 0;
2832
2833        sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2834            sas_device_priv_data->sas_target->handle);
2835        sas_device_priv_data->block = 1;
2836
2837        r = scsi_internal_device_block(sdev);
2838        if (r == -EINVAL)
2839                sdev_printk(KERN_WARNING, sdev,
2840                    "device_block failed with return(%d) for handle(0x%04x)\n",
2841                    sas_device_priv_data->sas_target->handle, r);
2842}
2843
2844/**
2845 * _scsih_internal_device_unblock - unblock the sdev device
2846 * @sdev: per device object
2847 * @sas_device_priv_data : per device driver private data
2848 * make sure device is unblocked without error, if not retry
2849 * by blocking and then unblocking
2850 */
2851
2852static void
2853_scsih_internal_device_unblock(struct scsi_device *sdev,
2854                        struct MPT3SAS_DEVICE *sas_device_priv_data)
2855{
2856        int r = 0;
2857
2858        sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2859            "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2860        sas_device_priv_data->block = 0;
2861        r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2862        if (r == -EINVAL) {
2863                /* The device has been set to SDEV_RUNNING by SD layer during
2864                 * device addition but the request queue is still stopped by
2865                 * our earlier block call. We need to perform a block again
2866                 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2867
2868                sdev_printk(KERN_WARNING, sdev,
2869                    "device_unblock failed with return(%d) for handle(0x%04x) "
2870                    "performing a block followed by an unblock\n",
2871                    sas_device_priv_data->sas_target->handle, r);
2872                sas_device_priv_data->block = 1;
2873                r = scsi_internal_device_block(sdev);
2874                if (r)
2875                        sdev_printk(KERN_WARNING, sdev, "retried device_block "
2876                            "failed with return(%d) for handle(0x%04x)\n",
2877                            sas_device_priv_data->sas_target->handle, r);
2878
2879                sas_device_priv_data->block = 0;
2880                r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2881                if (r)
2882                        sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2883                            " failed with return(%d) for handle(0x%04x)\n",
2884                            sas_device_priv_data->sas_target->handle, r);
2885        }
2886}
2887
2888/**
2889 * _scsih_ublock_io_all_device - unblock every device
2890 * @ioc: per adapter object
2891 *
2892 * change the device state from block to running
2893 */
2894static void
2895_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2896{
2897        struct MPT3SAS_DEVICE *sas_device_priv_data;
2898        struct scsi_device *sdev;
2899
2900        shost_for_each_device(sdev, ioc->shost) {
2901                sas_device_priv_data = sdev->hostdata;
2902                if (!sas_device_priv_data)
2903                        continue;
2904                if (!sas_device_priv_data->block)
2905                        continue;
2906
2907                dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2908                        "device_running, handle(0x%04x)\n",
2909                    sas_device_priv_data->sas_target->handle));
2910                _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2911        }
2912}
2913
2914
2915/**
2916 * _scsih_ublock_io_device - prepare device to be deleted
2917 * @ioc: per adapter object
2918 * @sas_addr: sas address
2919 *
2920 * unblock then put device in offline state
2921 */
2922static void
2923_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2924{
2925        struct MPT3SAS_DEVICE *sas_device_priv_data;
2926        struct scsi_device *sdev;
2927
2928        shost_for_each_device(sdev, ioc->shost) {
2929                sas_device_priv_data = sdev->hostdata;
2930                if (!sas_device_priv_data)
2931                        continue;
2932                if (sas_device_priv_data->sas_target->sas_address
2933                    != sas_address)
2934                        continue;
2935                if (sas_device_priv_data->block)
2936                        _scsih_internal_device_unblock(sdev,
2937                                sas_device_priv_data);
2938        }
2939}
2940
2941/**
2942 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2943 * @ioc: per adapter object
2944 * @handle: device handle
2945 *
2946 * During device pull we need to appropiately set the sdev state.
2947 */
2948static void
2949_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2950{
2951        struct MPT3SAS_DEVICE *sas_device_priv_data;
2952        struct scsi_device *sdev;
2953
2954        shost_for_each_device(sdev, ioc->shost) {
2955                sas_device_priv_data = sdev->hostdata;
2956                if (!sas_device_priv_data)
2957                        continue;
2958                if (sas_device_priv_data->block)
2959                        continue;
2960                if (sas_device_priv_data->ignore_delay_remove) {
2961                        sdev_printk(KERN_INFO, sdev,
2962                        "%s skip device_block for SES handle(0x%04x)\n",
2963                        __func__, sas_device_priv_data->sas_target->handle);
2964                        continue;
2965                }
2966                _scsih_internal_device_block(sdev, sas_device_priv_data);
2967        }
2968}
2969
2970/**
2971 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2972 * @ioc: per adapter object
2973 * @handle: device handle
2974 *
2975 * During device pull we need to appropiately set the sdev state.
2976 */
2977static void
2978_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2979{
2980        struct MPT3SAS_DEVICE *sas_device_priv_data;
2981        struct scsi_device *sdev;
2982        struct _sas_device *sas_device;
2983
2984        sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
2985        if (!sas_device)
2986                return;
2987
2988        shost_for_each_device(sdev, ioc->shost) {
2989                sas_device_priv_data = sdev->hostdata;
2990                if (!sas_device_priv_data)
2991                        continue;
2992                if (sas_device_priv_data->sas_target->handle != handle)
2993                        continue;
2994                if (sas_device_priv_data->block)
2995                        continue;
2996                if (sas_device->pend_sas_rphy_add)
2997                        continue;
2998                if (sas_device_priv_data->ignore_delay_remove) {
2999                        sdev_printk(KERN_INFO, sdev,
3000                        "%s skip device_block for SES handle(0x%04x)\n",
3001                        __func__, sas_device_priv_data->sas_target->handle);
3002                        continue;
3003                }
3004                _scsih_internal_device_block(sdev, sas_device_priv_data);
3005        }
3006
3007        sas_device_put(sas_device);
3008}
3009
3010/**
3011 * _scsih_block_io_to_children_attached_to_ex
3012 * @ioc: per adapter object
3013 * @sas_expander: the sas_device object
3014 *
3015 * This routine set sdev state to SDEV_BLOCK for all devices
3016 * attached to this expander. This function called when expander is
3017 * pulled.
3018 */
3019static void
3020_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3021        struct _sas_node *sas_expander)
3022{
3023        struct _sas_port *mpt3sas_port;
3024        struct _sas_device *sas_device;
3025        struct _sas_node *expander_sibling;
3026        unsigned long flags;
3027
3028        if (!sas_expander)
3029                return;
3030
3031        list_for_each_entry(mpt3sas_port,
3032           &sas_expander->sas_port_list, port_list) {
3033                if (mpt3sas_port->remote_identify.device_type ==
3034                    SAS_END_DEVICE) {
3035                        spin_lock_irqsave(&ioc->sas_device_lock, flags);
3036                        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3037                            mpt3sas_port->remote_identify.sas_address);
3038                        if (sas_device) {
3039                                set_bit(sas_device->handle,
3040                                                ioc->blocking_handles);
3041                                sas_device_put(sas_device);
3042                        }
3043                        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3044                }
3045        }
3046
3047        list_for_each_entry(mpt3sas_port,
3048           &sas_expander->sas_port_list, port_list) {
3049
3050                if (mpt3sas_port->remote_identify.device_type ==
3051                    SAS_EDGE_EXPANDER_DEVICE ||
3052                    mpt3sas_port->remote_identify.device_type ==
3053                    SAS_FANOUT_EXPANDER_DEVICE) {
3054                        expander_sibling =
3055                            mpt3sas_scsih_expander_find_by_sas_address(
3056                            ioc, mpt3sas_port->remote_identify.sas_address);
3057                        _scsih_block_io_to_children_attached_to_ex(ioc,
3058                            expander_sibling);
3059                }
3060        }
3061}
3062
3063/**
3064 * _scsih_block_io_to_children_attached_directly
3065 * @ioc: per adapter object
3066 * @event_data: topology change event data
3067 *
3068 * This routine set sdev state to SDEV_BLOCK for all devices
3069 * direct attached during device pull.
3070 */
3071static void
3072_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3073        Mpi2EventDataSasTopologyChangeList_t *event_data)
3074{
3075        int i;
3076        u16 handle;
3077        u16 reason_code;
3078
3079        for (i = 0; i < event_data->NumEntries; i++) {
3080                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3081                if (!handle)
3082                        continue;
3083                reason_code = event_data->PHY[i].PhyStatus &
3084                    MPI2_EVENT_SAS_TOPO_RC_MASK;
3085                if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3086                        _scsih_block_io_device(ioc, handle);
3087        }
3088}
3089
3090/**
3091 * _scsih_tm_tr_send - send task management request
3092 * @ioc: per adapter object
3093 * @handle: device handle
3094 * Context: interrupt time.
3095 *
3096 * This code is to initiate the device removal handshake protocol
3097 * with controller firmware.  This function will issue target reset
3098 * using high priority request queue.  It will send a sas iounit
3099 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3100 *
3101 * This is designed to send muliple task management request at the same
3102 * time to the fifo. If the fifo is full, we will append the request,
3103 * and process it in a future completion.
3104 */
3105static void
3106_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3107{
3108        Mpi2SCSITaskManagementRequest_t *mpi_request;
3109        u16 smid;
3110        struct _sas_device *sas_device = NULL;
3111        struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3112        u64 sas_address = 0;
3113        unsigned long flags;
3114        struct _tr_list *delayed_tr;
3115        u32 ioc_state;
3116
3117        if (ioc->remove_host) {
3118                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3119                        "%s: host has been removed: handle(0x%04x)\n",
3120                        __func__, ioc->name, handle));
3121                return;
3122        } else if (ioc->pci_error_recovery) {
3123                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3124                        "%s: host in pci error recovery: handle(0x%04x)\n",
3125                        __func__, ioc->name,
3126                    handle));
3127                return;
3128        }
3129        ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3130        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3131                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3132                        "%s: host is not operational: handle(0x%04x)\n",
3133                        __func__, ioc->name,
3134                   handle));
3135                return;
3136        }
3137
3138        /* if PD, then return */
3139        if (test_bit(handle, ioc->pd_handles))
3140                return;
3141
3142        spin_lock_irqsave(&ioc->sas_device_lock, flags);
3143        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3144        if (sas_device && sas_device->starget &&
3145            sas_device->starget->hostdata) {
3146                sas_target_priv_data = sas_device->starget->hostdata;
3147                sas_target_priv_data->deleted = 1;
3148                sas_address = sas_device->sas_address;
3149        }
3150        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3151
3152        if (sas_target_priv_data) {
3153                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3154                        "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3155                        ioc->name, handle,
3156                    (unsigned long long)sas_address));
3157                if (sas_device->enclosure_handle != 0)
3158                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3159                         "setting delete flag:enclosure logical id(0x%016llx),"
3160                         " slot(%d)\n", ioc->name, (unsigned long long)
3161                          sas_device->enclosure_logical_id,
3162                          sas_device->slot));
3163                if (sas_device->connector_name[0] != '\0')
3164                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3165                         "setting delete flag: enclosure level(0x%04x),"
3166                         " connector name( %s)\n", ioc->name,
3167                          sas_device->enclosure_level,
3168                          sas_device->connector_name));
3169                _scsih_ublock_io_device(ioc, sas_address);
3170                sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3171        }
3172
3173        smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3174        if (!smid) {
3175                delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3176                if (!delayed_tr)
3177                        goto out;
3178                INIT_LIST_HEAD(&delayed_tr->list);
3179                delayed_tr->handle = handle;
3180                list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3181                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3182                    "DELAYED:tr:handle(0x%04x), (open)\n",
3183                    ioc->name, handle));
3184                goto out;
3185        }
3186
3187        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3188                "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3189                ioc->name, handle, smid,
3190            ioc->tm_tr_cb_idx));
3191        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3192        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3193        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3194        mpi_request->DevHandle = cpu_to_le16(handle);
3195        mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3196        mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3197        mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3198
3199out:
3200        if (sas_device)
3201                sas_device_put(sas_device);
3202}
3203
3204/**
3205 * _scsih_tm_tr_complete -
3206 * @ioc: per adapter object
3207 * @smid: system request message index
3208 * @msix_index: MSIX table index supplied by the OS
3209 * @reply: reply message frame(lower 32bit addr)
3210 * Context: interrupt time.
3211 *
3212 * This is the target reset completion routine.
3213 * This code is part of the code to initiate the device removal
3214 * handshake protocol with controller firmware.
3215 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3216 *
3217 * Return 1 meaning mf should be freed from _base_interrupt
3218 *        0 means the mf is freed from this function.
3219 */
3220static u8
3221_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3222        u32 reply)
3223{
3224        u16 handle;
3225        Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3226        Mpi2SCSITaskManagementReply_t *mpi_reply =
3227            mpt3sas_base_get_reply_virt_addr(ioc, reply);
3228        Mpi2SasIoUnitControlRequest_t *mpi_request;
3229        u16 smid_sas_ctrl;
3230        u32 ioc_state;
3231        struct _sc_list *delayed_sc;
3232
3233        if (ioc->remove_host) {
3234                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3235                        "%s: host has been removed\n", __func__, ioc->name));
3236                return 1;
3237        } else if (ioc->pci_error_recovery) {
3238                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3239                        "%s: host in pci error recovery\n", __func__,
3240                        ioc->name));
3241                return 1;
3242        }
3243        ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3244        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3245                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3246                        "%s: host is not operational\n", __func__, ioc->name));
3247                return 1;
3248        }
3249        if (unlikely(!mpi_reply)) {
3250                pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3251                    ioc->name, __FILE__, __LINE__, __func__);
3252                return 1;
3253        }
3254        mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3255        handle = le16_to_cpu(mpi_request_tm->DevHandle);
3256        if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3257                dewtprintk(ioc, pr_err(MPT3SAS_FMT
3258                        "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3259                        ioc->name, handle,
3260                    le16_to_cpu(mpi_reply->DevHandle), smid));
3261                return 0;
3262        }
3263
3264        mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3265        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3266            "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3267            "loginfo(0x%08x), completed(%d)\n", ioc->name,
3268            handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3269            le32_to_cpu(mpi_reply->IOCLogInfo),
3270            le32_to_cpu(mpi_reply->TerminationCount)));
3271
3272        smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3273        if (!smid_sas_ctrl) {
3274                delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3275                if (!delayed_sc)
3276                        return _scsih_check_for_pending_tm(ioc, smid);
3277                INIT_LIST_HEAD(&delayed_sc->list);
3278                delayed_sc->handle = mpi_request_tm->DevHandle;
3279                list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3280                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3281                    "DELAYED:sc:handle(0x%04x), (open)\n",
3282                    ioc->name, handle));
3283                return _scsih_check_for_pending_tm(ioc, smid);
3284        }
3285
3286        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3287                "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3288                ioc->name, handle, smid_sas_ctrl,
3289            ioc->tm_sas_control_cb_idx));
3290        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3291        memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3292        mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3293        mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3294        mpi_request->DevHandle = mpi_request_tm->DevHandle;
3295        mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3296
3297        return _scsih_check_for_pending_tm(ioc, smid);
3298}
3299
3300
3301/**
3302 * _scsih_sas_control_complete - completion routine
3303 * @ioc: per adapter object
3304 * @smid: system request message index
3305 * @msix_index: MSIX table index supplied by the OS
3306 * @reply: reply message frame(lower 32bit addr)
3307 * Context: interrupt time.
3308 *
3309 * This is the sas iounit control completion routine.
3310 * This code is part of the code to initiate the device removal
3311 * handshake protocol with controller firmware.
3312 *
3313 * Return 1 meaning mf should be freed from _base_interrupt
3314 *        0 means the mf is freed from this function.
3315 */
3316static u8
3317_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3318        u8 msix_index, u32 reply)
3319{
3320        Mpi2SasIoUnitControlReply_t *mpi_reply =
3321            mpt3sas_base_get_reply_virt_addr(ioc, reply);
3322
3323        if (likely(mpi_reply)) {
3324                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3325                "sc_complete:handle(0x%04x), (open) "
3326                "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3327                ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3328                le16_to_cpu(mpi_reply->IOCStatus),
3329                le32_to_cpu(mpi_reply->IOCLogInfo)));
3330        } else {
3331                pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3332                    ioc->name, __FILE__, __LINE__, __func__);
3333        }
3334        return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
3335}
3336
3337/**
3338 * _scsih_tm_tr_volume_send - send target reset request for volumes
3339 * @ioc: per adapter object
3340 * @handle: device handle
3341 * Context: interrupt time.
3342 *
3343 * This is designed to send muliple task management request at the same
3344 * time to the fifo. If the fifo is full, we will append the request,
3345 * and process it in a future completion.
3346 */
3347static void
3348_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3349{
3350        Mpi2SCSITaskManagementRequest_t *mpi_request;
3351        u16 smid;
3352        struct _tr_list *delayed_tr;
3353
3354        if (ioc->shost_recovery || ioc->remove_host ||
3355            ioc->pci_error_recovery) {
3356                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3357                        "%s: host reset in progress!\n",
3358                        __func__, ioc->name));
3359                return;
3360        }
3361
3362        smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3363        if (!smid) {
3364                delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3365                if (!delayed_tr)
3366                        return;
3367                INIT_LIST_HEAD(&delayed_tr->list);
3368                delayed_tr->handle = handle;
3369                list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3370                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3371                    "DELAYED:tr:handle(0x%04x), (open)\n",
3372                    ioc->name, handle));
3373                return;
3374        }
3375
3376        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3377                "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3378                ioc->name, handle, smid,
3379            ioc->tm_tr_volume_cb_idx));
3380        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3381        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3382        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3383        mpi_request->DevHandle = cpu_to_le16(handle);
3384        mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3385        mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3386}
3387
3388/**
3389 * _scsih_tm_volume_tr_complete - target reset completion
3390 * @ioc: per adapter object
3391 * @smid: system request message index
3392 * @msix_index: MSIX table index supplied by the OS
3393 * @reply: reply message frame(lower 32bit addr)
3394 * Context: interrupt time.
3395 *
3396 * Return 1 meaning mf should be freed from _base_interrupt
3397 *        0 means the mf is freed from this function.
3398 */
3399static u8
3400_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3401        u8 msix_index, u32 reply)
3402{
3403        u16 handle;
3404        Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3405        Mpi2SCSITaskManagementReply_t *mpi_reply =
3406            mpt3sas_base_get_reply_virt_addr(ioc, reply);
3407
3408        if (ioc->shost_recovery || ioc->remove_host ||
3409            ioc->pci_error_recovery) {
3410                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3411                        "%s: host reset in progress!\n",
3412                        __func__, ioc->name));
3413                return 1;
3414        }
3415        if (unlikely(!mpi_reply)) {
3416                pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3417                    ioc->name, __FILE__, __LINE__, __func__);
3418                return 1;
3419        }
3420
3421        mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3422        handle = le16_to_cpu(mpi_request_tm->DevHandle);
3423        if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3424                dewtprintk(ioc, pr_err(MPT3SAS_FMT
3425                        "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3426                        ioc->name, handle,
3427                    le16_to_cpu(mpi_reply->DevHandle), smid));
3428                return 0;
3429        }
3430
3431        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3432            "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3433            "loginfo(0x%08x), completed(%d)\n", ioc->name,
3434            handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3435            le32_to_cpu(mpi_reply->IOCLogInfo),
3436            le32_to_cpu(mpi_reply->TerminationCount)));
3437
3438        return _scsih_check_for_pending_tm(ioc, smid);
3439}
3440
3441/**
3442 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3443 * @ioc: per adapter object
3444 * @smid: system request message index
3445 * @event: Event ID
3446 * @event_context: used to track events uniquely
3447 *
3448 * Context - processed in interrupt context.
3449 */
3450void
3451_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3452                                u32 event_context)
3453{
3454        Mpi2EventAckRequest_t *ack_request;
3455        int i = smid - ioc->internal_smid;
3456        unsigned long flags;
3457
3458        /* Without releasing the smid just update the
3459         * call back index and reuse the same smid for
3460         * processing this delayed request
3461         */
3462        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3463        ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3464        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3465
3466        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3467                "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3468                ioc->name, le16_to_cpu(event), smid,
3469                ioc->base_cb_idx));
3470        ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3471        memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3472        ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3473        ack_request->Event = event;
3474        ack_request->EventContext = event_context;
3475        ack_request->VF_ID = 0;  /* TODO */
3476        ack_request->VP_ID = 0;
3477        mpt3sas_base_put_smid_default(ioc, smid);
3478}
3479
3480/**
3481 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3482 *                              sas_io_unit_ctrl messages
3483 * @ioc: per adapter object
3484 * @smid: system request message index
3485 * @handle: device handle
3486 *
3487 * Context - processed in interrupt context.
3488 */
3489void
3490_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3491                                        u16 smid, u16 handle)
3492        {
3493                Mpi2SasIoUnitControlRequest_t *mpi_request;
3494                u32 ioc_state;
3495                int i = smid - ioc->internal_smid;
3496                unsigned long flags;
3497
3498                if (ioc->remove_host) {
3499                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3500                            "%s: host has been removed\n",
3501                             __func__, ioc->name));
3502                        return;
3503                } else if (ioc->pci_error_recovery) {
3504                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3505                            "%s: host in pci error recovery\n",
3506                            __func__, ioc->name));
3507                return;
3508        }
3509        ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3510        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3511                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3512                    "%s: host is not operational\n",
3513                    __func__, ioc->name));
3514                return;
3515        }
3516
3517        /* Without releasing the smid just update the
3518         * call back index and reuse the same smid for
3519         * processing this delayed request
3520         */
3521        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3522        ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3523        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3524
3525        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3526            "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3527            ioc->name, le16_to_cpu(handle), smid,
3528            ioc->tm_sas_control_cb_idx));
3529        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3530        memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3531        mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3532        mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3533        mpi_request->DevHandle = handle;
3534        mpt3sas_base_put_smid_default(ioc, smid);
3535}
3536
3537/**
3538 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3539 * @ioc: per adapter object
3540 * @smid: system request message index
3541 *
3542 * Context: Executed in interrupt context
3543 *
3544 * This will check delayed internal messages list, and process the
3545 * next request.
3546 *
3547 * Return 1 meaning mf should be freed from _base_interrupt
3548 *        0 means the mf is freed from this function.
3549 */
3550u8
3551mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3552{
3553        struct _sc_list *delayed_sc;
3554        struct _event_ack_list *delayed_event_ack;
3555
3556        if (!list_empty(&ioc->delayed_event_ack_list)) {
3557                delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3558                                                struct _event_ack_list, list);
3559                _scsih_issue_delayed_event_ack(ioc, smid,
3560                  delayed_event_ack->Event, delayed_event_ack->EventContext);
3561                list_del(&delayed_event_ack->list);
3562                kfree(delayed_event_ack);
3563                return 0;
3564        }
3565
3566        if (!list_empty(&ioc->delayed_sc_list)) {
3567                delayed_sc = list_entry(ioc->delayed_sc_list.next,
3568                                                struct _sc_list, list);
3569                _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3570                                                 delayed_sc->handle);
3571                list_del(&delayed_sc->list);
3572                kfree(delayed_sc);
3573                return 0;
3574        }
3575        return 1;
3576}
3577
3578/**
3579 * _scsih_check_for_pending_tm - check for pending task management
3580 * @ioc: per adapter object
3581 * @smid: system request message index
3582 *
3583 * This will check delayed target reset list, and feed the
3584 * next reqeust.
3585 *
3586 * Return 1 meaning mf should be freed from _base_interrupt
3587 *        0 means the mf is freed from this function.
3588 */
3589static u8
3590_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3591{
3592        struct _tr_list *delayed_tr;
3593
3594        if (!list_empty(&ioc->delayed_tr_volume_list)) {
3595                delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3596                    struct _tr_list, list);
3597                mpt3sas_base_free_smid(ioc, smid);
3598                _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3599                list_del(&delayed_tr->list);
3600                kfree(delayed_tr);
3601                return 0;
3602        }
3603
3604        if (!list_empty(&ioc->delayed_tr_list)) {
3605                delayed_tr = list_entry(ioc->delayed_tr_list.next,
3606                    struct _tr_list, list);
3607                mpt3sas_base_free_smid(ioc, smid);
3608                _scsih_tm_tr_send(ioc, delayed_tr->handle);
3609                list_del(&delayed_tr->list);
3610                kfree(delayed_tr);
3611                return 0;
3612        }
3613
3614        return 1;
3615}
3616
3617/**
3618 * _scsih_check_topo_delete_events - sanity check on topo events
3619 * @ioc: per adapter object
3620 * @event_data: the event data payload
3621 *
3622 * This routine added to better handle cable breaker.
3623 *
3624 * This handles the case where driver receives multiple expander
3625 * add and delete events in a single shot.  When there is a delete event
3626 * the routine will void any pending add events waiting in the event queue.
3627 *
3628 * Return nothing.
3629 */
3630static void
3631_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3632        Mpi2EventDataSasTopologyChangeList_t *event_data)
3633{
3634        struct fw_event_work *fw_event;
3635        Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3636        u16 expander_handle;
3637        struct _sas_node *sas_expander;
3638        unsigned long flags;
3639        int i, reason_code;
3640        u16 handle;
3641
3642        for (i = 0 ; i < event_data->NumEntries; i++) {
3643                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3644                if (!handle)
3645                        continue;
3646                reason_code = event_data->PHY[i].PhyStatus &
3647                    MPI2_EVENT_SAS_TOPO_RC_MASK;
3648                if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3649                        _scsih_tm_tr_send(ioc, handle);
3650        }
3651
3652        expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3653        if (expander_handle < ioc->sas_hba.num_phys) {
3654                _scsih_block_io_to_children_attached_directly(ioc, event_data);
3655                return;
3656        }
3657        if (event_data->ExpStatus ==
3658            MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3659                /* put expander attached devices into blocking state */
3660                spin_lock_irqsave(&ioc->sas_node_lock, flags);
3661                sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3662                    expander_handle);
3663                _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3664                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3665                do {
3666                        handle = find_first_bit(ioc->blocking_handles,
3667                            ioc->facts.MaxDevHandle);
3668                        if (handle < ioc->facts.MaxDevHandle)
3669                                _scsih_block_io_device(ioc, handle);
3670                } while (test_and_clear_bit(handle, ioc->blocking_handles));
3671        } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3672                _scsih_block_io_to_children_attached_directly(ioc, event_data);
3673
3674        if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3675                return;
3676
3677        /* mark ignore flag for pending events */
3678        spin_lock_irqsave(&ioc->fw_event_lock, flags);
3679        list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3680                if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3681                    fw_event->ignore)
3682                        continue;
3683                local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3684                                   fw_event->event_data;
3685                if (local_event_data->ExpStatus ==
3686                    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3687                    local_event_data->ExpStatus ==
3688                    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3689                        if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3690                            expander_handle) {
3691                                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3692                                    "setting ignoring flag\n", ioc->name));
3693                                fw_event->ignore = 1;
3694                        }
3695                }
3696        }
3697        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3698}
3699
3700/**
3701 * _scsih_set_volume_delete_flag - setting volume delete flag
3702 * @ioc: per adapter object
3703 * @handle: device handle
3704 *
3705 * This returns nothing.
3706 */
3707static void
3708_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3709{
3710        struct _raid_device *raid_device;
3711        struct MPT3SAS_TARGET *sas_target_priv_data;
3712        unsigned long flags;
3713
3714        spin_lock_irqsave(&ioc->raid_device_lock, flags);
3715        raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3716        if (raid_device && raid_device->starget &&
3717            raid_device->starget->hostdata) {
3718                sas_target_priv_data =
3719                    raid_device->starget->hostdata;
3720                sas_target_priv_data->deleted = 1;
3721                dewtprintk(ioc, pr_info(MPT3SAS_FMT
3722                    "setting delete flag: handle(0x%04x), "
3723                    "wwid(0x%016llx)\n", ioc->name, handle,
3724                    (unsigned long long) raid_device->wwid));
3725        }
3726        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3727}
3728
3729/**
3730 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3731 * @handle: input handle
3732 * @a: handle for volume a
3733 * @b: handle for volume b
3734 *
3735 * IR firmware only supports two raid volumes.  The purpose of this
3736 * routine is to set the volume handle in either a or b. When the given
3737 * input handle is non-zero, or when a and b have not been set before.
3738 */
3739static void
3740_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3741{
3742        if (!handle || handle == *a || handle == *b)
3743                return;
3744        if (!*a)
3745                *a = handle;
3746        else if (!*b)
3747                *b = handle;
3748}
3749
3750/**
3751 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3752 * @ioc: per adapter object
3753 * @event_data: the event data payload
3754 * Context: interrupt time.
3755 *
3756 * This routine will send target reset to volume, followed by target
3757 * resets to the PDs. This is called when a PD has been removed, or
3758 * volume has been deleted or removed. When the target reset is sent
3759 * to volume, the PD target resets need to be queued to start upon
3760 * completion of the volume target reset.
3761 *
3762 * Return nothing.
3763 */
3764static void
3765_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3766        Mpi2EventDataIrConfigChangeList_t *event_data)
3767{
3768        Mpi2EventIrConfigElement_t *element;
3769        int i;
3770        u16 handle, volume_handle, a, b;
3771        struct _tr_list *delayed_tr;
3772
3773        a = 0;
3774        b = 0;
3775
3776        if (ioc->is_warpdrive)
3777                return;
3778
3779        /* Volume Resets for Deleted or Removed */
3780        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3781        for (i = 0; i < event_data->NumElements; i++, element++) {
3782                if (le32_to_cpu(event_data->Flags) &
3783                    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3784                        continue;
3785                if (element->ReasonCode ==
3786                    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3787                    element->ReasonCode ==
3788                    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3789                        volume_handle = le16_to_cpu(element->VolDevHandle);
3790                        _scsih_set_volume_delete_flag(ioc, volume_handle);
3791                        _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3792                }
3793        }
3794
3795        /* Volume Resets for UNHIDE events */
3796        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3797        for (i = 0; i < event_data->NumElements; i++, element++) {
3798                if (le32_to_cpu(event_data->Flags) &
3799                    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3800                        continue;
3801                if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3802                        volume_handle = le16_to_cpu(element->VolDevHandle);
3803                        _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3804                }
3805        }
3806
3807        if (a)
3808                _scsih_tm_tr_volume_send(ioc, a);
3809        if (b)
3810                _scsih_tm_tr_volume_send(ioc, b);
3811
3812        /* PD target resets */
3813        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3814        for (i = 0; i < event_data->NumElements; i++, element++) {
3815                if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3816                        continue;
3817                handle = le16_to_cpu(element->PhysDiskDevHandle);
3818                volume_handle = le16_to_cpu(element->VolDevHandle);
3819                clear_bit(handle, ioc->pd_handles);
3820                if (!volume_handle)
3821                        _scsih_tm_tr_send(ioc, handle);
3822                else if (volume_handle == a || volume_handle == b) {
3823                        delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3824                        BUG_ON(!delayed_tr);
3825                        INIT_LIST_HEAD(&delayed_tr->list);
3826                        delayed_tr->handle = handle;
3827                        list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3828                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
3829                            "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3830                            handle));
3831                } else
3832                        _scsih_tm_tr_send(ioc, handle);
3833        }
3834}
3835
3836
3837/**
3838 * _scsih_check_volume_delete_events - set delete flag for volumes
3839 * @ioc: per adapter object
3840 * @event_data: the event data payload
3841 * Context: interrupt time.
3842 *
3843 * This will handle the case when the cable connected to entire volume is
3844 * pulled. We will take care of setting the deleted flag so normal IO will
3845 * not be sent.
3846 *
3847 * Return nothing.
3848 */
3849static void
3850_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3851        Mpi2EventDataIrVolume_t *event_data)
3852{
3853        u32 state;
3854
3855        if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3856                return;
3857        state = le32_to_cpu(event_data->NewValue);
3858        if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3859            MPI2_RAID_VOL_STATE_FAILED)
3860                _scsih_set_volume_delete_flag(ioc,
3861                    le16_to_cpu(event_data->VolDevHandle));
3862}
3863
3864/**
3865 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3866 * @ioc: per adapter object
3867 * @event_data: the temp threshold event data
3868 * Context: interrupt time.
3869 *
3870 * Return nothing.
3871 */
3872static void
3873_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3874        Mpi2EventDataTemperature_t *event_data)
3875{
3876        if (ioc->temp_sensors_count >= event_data->SensorNum) {
3877                pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3878                  " exceeded for Sensor: %d !!!\n", ioc->name,
3879                  ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3880                  ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3881                  ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3882                  ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3883                  event_data->SensorNum);
3884                pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3885                        ioc->name, event_data->CurrentTemperature);
3886        }
3887}
3888
3889/**
3890 * _scsih_flush_running_cmds - completing outstanding commands.
3891 * @ioc: per adapter object
3892 *
3893 * The flushing out of all pending scmd commands following host reset,
3894 * where all IO is dropped to the floor.
3895 *
3896 * Return nothing.
3897 */
3898static void
3899_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3900{
3901        struct scsi_cmnd *scmd;
3902        u16 smid;
3903        u16 count = 0;
3904
3905        for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3906                scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3907                if (!scmd)
3908                        continue;
3909                count++;
3910                mpt3sas_base_free_smid(ioc, smid);
3911                scsi_dma_unmap(scmd);
3912                if (ioc->pci_error_recovery)
3913                        scmd->result = DID_NO_CONNECT << 16;
3914                else
3915                        scmd->result = DID_RESET << 16;
3916                scmd->scsi_done(scmd);
3917        }
3918        dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3919            ioc->name, count));
3920}
3921
3922/**
3923 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3924 * @ioc: per adapter object
3925 * @scmd: pointer to scsi command object
3926 * @mpi_request: pointer to the SCSI_IO reqest message frame
3927 *
3928 * Supporting protection 1 and 3.
3929 *
3930 * Returns nothing
3931 */
3932static void
3933_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3934        Mpi2SCSIIORequest_t *mpi_request)
3935{
3936        u16 eedp_flags;
3937        unsigned char prot_op = scsi_get_prot_op(scmd);
3938        unsigned char prot_type = scsi_get_prot_type(scmd);
3939        Mpi25SCSIIORequest_t *mpi_request_3v =
3940           (Mpi25SCSIIORequest_t *)mpi_request;
3941
3942        if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3943                return;
3944
3945        if (prot_op ==  SCSI_PROT_READ_STRIP)
3946                eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3947        else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3948                eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3949        else
3950                return;
3951
3952        switch (prot_type) {
3953        case SCSI_PROT_DIF_TYPE1:
3954        case SCSI_PROT_DIF_TYPE2:
3955
3956                /*
3957                * enable ref/guard checking
3958                * auto increment ref tag
3959                */
3960                eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3961                    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3962                    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3963                mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3964                    cpu_to_be32(scsi_get_lba(scmd));
3965                break;
3966
3967        case SCSI_PROT_DIF_TYPE3:
3968
3969                /*
3970                * enable guard checking
3971                */
3972                eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3973
3974                break;
3975        }
3976
3977        mpi_request_3v->EEDPBlockSize =
3978            cpu_to_le16(scmd->device->sector_size);
3979        mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3980}
3981
3982/**
3983 * _scsih_eedp_error_handling - return sense code for EEDP errors
3984 * @scmd: pointer to scsi command object
3985 * @ioc_status: ioc status
3986 *
3987 * Returns nothing
3988 */
3989static void
3990_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3991{
3992        u8 ascq;
3993
3994        switch (ioc_status) {
3995        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3996                ascq = 0x01;
3997                break;
3998        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3999                ascq = 0x02;
4000                break;
4001        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4002                ascq = 0x03;
4003                break;
4004        default:
4005                ascq = 0x00;
4006                break;
4007        }
4008        scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4009            ascq);
4010        scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4011            SAM_STAT_CHECK_CONDITION;
4012}
4013
4014
4015
4016/**
4017 * scsih_qcmd - main scsi request entry point
4018 * @scmd: pointer to scsi command object
4019 * @done: function pointer to be invoked on completion
4020 *
4021 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4022 *
4023 * Returns 0 on success.  If there's a failure, return either:
4024 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4025 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4026 */
4027int
4028scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
4029{
4030        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
4031        struct MPT3SAS_DEVICE *sas_device_priv_data;
4032        struct MPT3SAS_TARGET *sas_target_priv_data;
4033        struct _raid_device *raid_device;
4034        Mpi2SCSIIORequest_t *mpi_request;
4035        u32 mpi_control;
4036        u16 smid;
4037        u16 handle;
4038
4039        if (ioc->logging_level & MPT_DEBUG_SCSI)
4040                scsi_print_command(scmd);
4041
4042        sas_device_priv_data = scmd->device->hostdata;
4043        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4044                scmd->result = DID_NO_CONNECT << 16;
4045                scmd->scsi_done(scmd);
4046                return 0;
4047        }
4048
4049        if (ioc->pci_error_recovery || ioc->remove_host) {
4050                scmd->result = DID_NO_CONNECT << 16;
4051                scmd->scsi_done(scmd);
4052                return 0;
4053        }
4054
4055        sas_target_priv_data = sas_device_priv_data->sas_target;
4056
4057        /* invalid device handle */
4058        handle = sas_target_priv_data->handle;
4059        if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4060                scmd->result = DID_NO_CONNECT << 16;
4061                scmd->scsi_done(scmd);
4062                return 0;
4063        }
4064
4065
4066        /* host recovery or link resets sent via IOCTLs */
4067        if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4068                return SCSI_MLQUEUE_HOST_BUSY;
4069
4070        /* device has been deleted */
4071        else if (sas_target_priv_data->deleted) {
4072                scmd->result = DID_NO_CONNECT << 16;
4073                scmd->scsi_done(scmd);
4074                return 0;
4075        /* device busy with task managment */
4076        } else if (sas_target_priv_data->tm_busy ||
4077            sas_device_priv_data->block)
4078                return SCSI_MLQUEUE_DEVICE_BUSY;
4079
4080        if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4081                mpi_control = MPI2_SCSIIO_CONTROL_READ;
4082        else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4083                mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4084        else
4085                mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4086
4087        /* set tags */
4088        mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4089
4090        /* Make sure Device is not raid volume.
4091         * We do not expose raid functionality to upper layer for warpdrive.
4092         */
4093        if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
4094            && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
4095                mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4096
4097        smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4098        if (!smid) {
4099                pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4100                    ioc->name, __func__);
4101                goto out;
4102        }
4103        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4104        memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4105        _scsih_setup_eedp(ioc, scmd, mpi_request);
4106
4107        if (scmd->cmd_len == 32)
4108                mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4109        mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4110        if (sas_device_priv_data->sas_target->flags &
4111            MPT_TARGET_FLAGS_RAID_COMPONENT)
4112                mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4113        else
4114                mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4115        mpi_request->DevHandle = cpu_to_le16(handle);
4116        mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4117        mpi_request->Control = cpu_to_le32(mpi_control);
4118        mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4119        mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4120        mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4121        mpi_request->SenseBufferLowAddress =
4122            mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4123        mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4124        int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4125            mpi_request->LUN);
4126        memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4127
4128        if (mpi_request->DataLength) {
4129                if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4130                        mpt3sas_base_free_smid(ioc, smid);
4131                        goto out;
4132                }
4133        } else
4134                ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4135
4136        raid_device = sas_target_priv_data->raid_device;
4137        if (raid_device && raid_device->direct_io_enabled)
4138                mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4139                    smid);
4140
4141        if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4142                if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4143                        mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4144                            MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4145                        mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
4146                } else
4147                        mpt3sas_base_put_smid_scsi_io(ioc, smid,
4148                            le16_to_cpu(mpi_request->DevHandle));
4149        } else
4150                mpt3sas_base_put_smid_default(ioc, smid);
4151        return 0;
4152
4153 out:
4154        return SCSI_MLQUEUE_HOST_BUSY;
4155}
4156
4157/**
4158 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4159 * @sense_buffer: sense data returned by target
4160 * @data: normalized skey/asc/ascq
4161 *
4162 * Return nothing.
4163 */
4164static void
4165_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4166{
4167        if ((sense_buffer[0] & 0x7F) >= 0x72) {
4168                /* descriptor format */
4169                data->skey = sense_buffer[1] & 0x0F;
4170                data->asc = sense_buffer[2];
4171                data->ascq = sense_buffer[3];
4172        } else {
4173                /* fixed format */
4174                data->skey = sense_buffer[2] & 0x0F;
4175                data->asc = sense_buffer[12];
4176                data->ascq = sense_buffer[13];
4177        }
4178}
4179
4180/**
4181 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4182 * @ioc: per adapter object
4183 * @scmd: pointer to scsi command object
4184 * @mpi_reply: reply mf payload returned from firmware
4185 *
4186 * scsi_status - SCSI Status code returned from target device
4187 * scsi_state - state info associated with SCSI_IO determined by ioc
4188 * ioc_status - ioc supplied status info
4189 *
4190 * Return nothing.
4191 */
4192static void
4193_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4194        Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4195{
4196        u32 response_info;
4197        u8 *response_bytes;
4198        u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4199            MPI2_IOCSTATUS_MASK;
4200        u8 scsi_state = mpi_reply->SCSIState;
4201        u8 scsi_status = mpi_reply->SCSIStatus;
4202        char *desc_ioc_state = NULL;
4203        char *desc_scsi_status = NULL;
4204        char *desc_scsi_state = ioc->tmp_string;
4205        u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4206        struct _sas_device *sas_device = NULL;
4207        struct scsi_target *starget = scmd->device->sdev_target;
4208        struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4209        char *device_str = NULL;
4210
4211        if (!priv_target)
4212                return;
4213        if (ioc->hide_ir_msg)
4214                device_str = "WarpDrive";
4215        else
4216                device_str = "volume";
4217
4218        if (log_info == 0x31170000)
4219                return;
4220
4221        switch (ioc_status) {
4222        case MPI2_IOCSTATUS_SUCCESS:
4223                desc_ioc_state = "success";
4224                break;
4225        case MPI2_IOCSTATUS_INVALID_FUNCTION:
4226                desc_ioc_state = "invalid function";
4227                break;
4228        case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4229                desc_ioc_state = "scsi recovered error";
4230                break;
4231        case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4232                desc_ioc_state = "scsi invalid dev handle";
4233                break;
4234        case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4235                desc_ioc_state = "scsi device not there";
4236                break;
4237        case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4238                desc_ioc_state = "scsi data overrun";
4239                break;
4240        case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4241                desc_ioc_state = "scsi data underrun";
4242                break;
4243        case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4244                desc_ioc_state = "scsi io data error";
4245                break;
4246        case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4247                desc_ioc_state = "scsi protocol error";
4248                break;
4249        case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4250                desc_ioc_state = "scsi task terminated";
4251                break;
4252        case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4253                desc_ioc_state = "scsi residual mismatch";
4254                break;
4255        case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4256                desc_ioc_state = "scsi task mgmt failed";
4257                break;
4258        case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4259                desc_ioc_state = "scsi ioc terminated";
4260                break;
4261        case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4262                desc_ioc_state = "scsi ext terminated";
4263                break;
4264        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4265                desc_ioc_state = "eedp guard error";
4266                break;
4267        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4268                desc_ioc_state = "eedp ref tag error";
4269                break;
4270        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4271                desc_ioc_state = "eedp app tag error";
4272                break;
4273        case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4274                desc_ioc_state = "insufficient power";
4275                break;
4276        default:
4277                desc_ioc_state = "unknown";
4278                break;
4279        }
4280
4281        switch (scsi_status) {
4282        case MPI2_SCSI_STATUS_GOOD:
4283                desc_scsi_status = "good";
4284                break;
4285        case MPI2_SCSI_STATUS_CHECK_CONDITION:
4286                desc_scsi_status = "check condition";
4287                break;
4288        case MPI2_SCSI_STATUS_CONDITION_MET:
4289                desc_scsi_status = "condition met";
4290                break;
4291        case MPI2_SCSI_STATUS_BUSY:
4292                desc_scsi_status = "busy";
4293                break;
4294        case MPI2_SCSI_STATUS_INTERMEDIATE:
4295                desc_scsi_status = "intermediate";
4296                break;
4297        case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4298                desc_scsi_status = "intermediate condmet";
4299                break;
4300        case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4301                desc_scsi_status = "reservation conflict";
4302                break;
4303        case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4304                desc_scsi_status = "command terminated";
4305                break;
4306        case MPI2_SCSI_STATUS_TASK_SET_FULL:
4307                desc_scsi_status = "task set full";
4308                break;
4309        case MPI2_SCSI_STATUS_ACA_ACTIVE:
4310                desc_scsi_status = "aca active";
4311                break;
4312        case MPI2_SCSI_STATUS_TASK_ABORTED:
4313                desc_scsi_status = "task aborted";
4314                break;
4315        default:
4316                desc_scsi_status = "unknown";
4317                break;
4318        }
4319
4320        desc_scsi_state[0] = '\0';
4321        if (!scsi_state)
4322                desc_scsi_state = " ";
4323        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4324                strcat(desc_scsi_state, "response info ");
4325        if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4326                strcat(desc_scsi_state, "state terminated ");
4327        if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4328                strcat(desc_scsi_state, "no status ");
4329        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4330                strcat(desc_scsi_state, "autosense failed ");
4331        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4332                strcat(desc_scsi_state, "autosense valid ");
4333
4334        scsi_print_command(scmd);
4335
4336        if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4337                pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4338                    device_str, (unsigned long long)priv_target->sas_address);
4339        } else {
4340                sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4341                if (sas_device) {
4342                        pr_warn(MPT3SAS_FMT
4343                                "\tsas_address(0x%016llx), phy(%d)\n",
4344                                ioc->name, (unsigned long long)
4345                            sas_device->sas_address, sas_device->phy);
4346                        if (sas_device->enclosure_handle != 0)
4347                                pr_warn(MPT3SAS_FMT
4348                                  "\tenclosure_logical_id(0x%016llx),"
4349                                  "slot(%d)\n", ioc->name,
4350                                  (unsigned long long)
4351                                  sas_device->enclosure_logical_id,
4352                                  sas_device->slot);
4353                        if (sas_device->connector_name[0])
4354                                pr_warn(MPT3SAS_FMT
4355                                  "\tenclosure level(0x%04x),"
4356                                  " connector name( %s)\n", ioc->name,
4357                                  sas_device->enclosure_level,
4358                                  sas_device->connector_name);
4359
4360                        sas_device_put(sas_device);
4361                }
4362        }
4363
4364        pr_warn(MPT3SAS_FMT
4365                "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4366                ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4367            desc_ioc_state, ioc_status, smid);
4368        pr_warn(MPT3SAS_FMT
4369                "\trequest_len(%d), underflow(%d), resid(%d)\n",
4370                ioc->name, scsi_bufflen(scmd), scmd->underflow,
4371            scsi_get_resid(scmd));
4372        pr_warn(MPT3SAS_FMT
4373                "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4374                ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4375            le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4376        pr_warn(MPT3SAS_FMT
4377                "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4378                ioc->name, desc_scsi_status,
4379            scsi_status, desc_scsi_state, scsi_state);
4380
4381        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4382                struct sense_info data;
4383                _scsih_normalize_sense(scmd->sense_buffer, &data);
4384                pr_warn(MPT3SAS_FMT
4385                        "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4386                        ioc->name, data.skey,
4387                    data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4388        }
4389
4390        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4391                response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4392                response_bytes = (u8 *)&response_info;
4393                _scsih_response_code(ioc, response_bytes[0]);
4394        }
4395}
4396
4397/**
4398 * _scsih_turn_on_pfa_led - illuminate PFA LED
4399 * @ioc: per adapter object
4400 * @handle: device handle
4401 * Context: process
4402 *
4403 * Return nothing.
4404 */
4405static void
4406_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4407{
4408        Mpi2SepReply_t mpi_reply;
4409        Mpi2SepRequest_t mpi_request;
4410        struct _sas_device *sas_device;
4411
4412        sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4413        if (!sas_device)
4414                return;
4415
4416        memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4417        mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4418        mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4419        mpi_request.SlotStatus =
4420            cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4421        mpi_request.DevHandle = cpu_to_le16(handle);
4422        mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4423        if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4424            &mpi_request)) != 0) {
4425                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4426                __FILE__, __LINE__, __func__);
4427                goto out;
4428        }
4429        sas_device->pfa_led_on = 1;
4430
4431        if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4432                dewtprintk(ioc, pr_info(MPT3SAS_FMT
4433                        "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4434                        ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4435                    le32_to_cpu(mpi_reply.IOCLogInfo)));
4436                goto out;
4437        }
4438out:
4439        sas_device_put(sas_device);
4440}
4441
4442/**
4443 * _scsih_turn_off_pfa_led - turn off Fault LED
4444 * @ioc: per adapter object
4445 * @sas_device: sas device whose PFA LED has to turned off
4446 * Context: process
4447 *
4448 * Return nothing.
4449 */
4450static void
4451_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4452        struct _sas_device *sas_device)
4453{
4454        Mpi2SepReply_t mpi_reply;
4455        Mpi2SepRequest_t mpi_request;
4456
4457        memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4458        mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4459        mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4460        mpi_request.SlotStatus = 0;
4461        mpi_request.Slot = cpu_to_le16(sas_device->slot);
4462        mpi_request.DevHandle = 0;
4463        mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4464        mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4465        if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4466                &mpi_request)) != 0) {
4467                printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4468                __FILE__, __LINE__, __func__);
4469                return;
4470        }
4471
4472        if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4473                dewtprintk(ioc, printk(MPT3SAS_FMT
4474                 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4475                 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4476                 le32_to_cpu(mpi_reply.IOCLogInfo)));
4477                return;
4478        }
4479}
4480
4481/**
4482 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4483 * @ioc: per adapter object
4484 * @handle: device handle
4485 * Context: interrupt.
4486 *
4487 * Return nothing.
4488 */
4489static void
4490_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4491{
4492        struct fw_event_work *fw_event;
4493
4494        fw_event = alloc_fw_event_work(0);
4495        if (!fw_event)
4496                return;
4497        fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4498        fw_event->device_handle = handle;
4499        fw_event->ioc = ioc;
4500        _scsih_fw_event_add(ioc, fw_event);
4501        fw_event_work_put(fw_event);
4502}
4503
4504/**
4505 * _scsih_smart_predicted_fault - process smart errors
4506 * @ioc: per adapter object
4507 * @handle: device handle
4508 * Context: interrupt.
4509 *
4510 * Return nothing.
4511 */
4512static void
4513_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4514{
4515        struct scsi_target *starget;
4516        struct MPT3SAS_TARGET *sas_target_priv_data;
4517        Mpi2EventNotificationReply_t *event_reply;
4518        Mpi2EventDataSasDeviceStatusChange_t *event_data;
4519        struct _sas_device *sas_device;
4520        ssize_t sz;
4521        unsigned long flags;
4522
4523        /* only handle non-raid devices */
4524        spin_lock_irqsave(&ioc->sas_device_lock, flags);
4525        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4526        if (!sas_device)
4527                goto out_unlock;
4528
4529        starget = sas_device->starget;
4530        sas_target_priv_data = starget->hostdata;
4531
4532        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4533           ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4534                goto out_unlock;
4535
4536        if (sas_device->enclosure_handle != 0)
4537                starget_printk(KERN_INFO, starget, "predicted fault, "
4538                        "enclosure logical id(0x%016llx), slot(%d)\n",
4539                        (unsigned long long)sas_device->enclosure_logical_id,
4540                        sas_device->slot);
4541        if (sas_device->connector_name[0] != '\0')
4542                starget_printk(KERN_WARNING, starget, "predicted fault, "
4543                        "enclosure level(0x%04x), connector name( %s)\n",
4544                        sas_device->enclosure_level,
4545                        sas_device->connector_name);
4546        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4547
4548        if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4549                _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4550
4551        /* insert into event log */
4552        sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4553             sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4554        event_reply = kzalloc(sz, GFP_KERNEL);
4555        if (!event_reply) {
4556                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4557                    ioc->name, __FILE__, __LINE__, __func__);
4558                goto out;
4559        }
4560
4561        event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4562        event_reply->Event =
4563            cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4564        event_reply->MsgLength = sz/4;
4565        event_reply->EventDataLength =
4566            cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4567        event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4568            event_reply->EventData;
4569        event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4570        event_data->ASC = 0x5D;
4571        event_data->DevHandle = cpu_to_le16(handle);
4572        event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4573        mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4574        kfree(event_reply);
4575out:
4576        if (sas_device)
4577                sas_device_put(sas_device);
4578        return;
4579
4580out_unlock:
4581        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4582        goto out;
4583}
4584
4585/**
4586 * _scsih_io_done - scsi request callback
4587 * @ioc: per adapter object
4588 * @smid: system request message index
4589 * @msix_index: MSIX table index supplied by the OS
4590 * @reply: reply message frame(lower 32bit addr)
4591 *
4592 * Callback handler when using _scsih_qcmd.
4593 *
4594 * Return 1 meaning mf should be freed from _base_interrupt
4595 *        0 means the mf is freed from this function.
4596 */
4597static u8
4598_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4599{
4600        Mpi2SCSIIORequest_t *mpi_request;
4601        Mpi2SCSIIOReply_t *mpi_reply;
4602        struct scsi_cmnd *scmd;
4603        u16 ioc_status;
4604        u32 xfer_cnt;
4605        u8 scsi_state;
4606        u8 scsi_status;
4607        u32 log_info;
4608        struct MPT3SAS_DEVICE *sas_device_priv_data;
4609        u32 response_code = 0;
4610        unsigned long flags;
4611
4612        mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4613        scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4614        if (scmd == NULL)
4615                return 1;
4616
4617        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4618
4619        if (mpi_reply == NULL) {
4620                scmd->result = DID_OK << 16;
4621                goto out;
4622        }
4623
4624        sas_device_priv_data = scmd->device->hostdata;
4625        if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4626             sas_device_priv_data->sas_target->deleted) {
4627                scmd->result = DID_NO_CONNECT << 16;
4628                goto out;
4629        }
4630        ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4631
4632        /*
4633         * WARPDRIVE: If direct_io is set then it is directIO,
4634         * the failed direct I/O should be redirected to volume
4635         */
4636        if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4637             ((ioc_status & MPI2_IOCSTATUS_MASK)
4638              != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4639                spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4640                ioc->scsi_lookup[smid - 1].scmd = scmd;
4641                spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4642                mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4643                memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4644                mpi_request->DevHandle =
4645                    cpu_to_le16(sas_device_priv_data->sas_target->handle);
4646                mpt3sas_base_put_smid_scsi_io(ioc, smid,
4647                    sas_device_priv_data->sas_target->handle);
4648                return 0;
4649        }
4650        /* turning off TLR */
4651        scsi_state = mpi_reply->SCSIState;
4652        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4653                response_code =
4654                    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4655        if (!sas_device_priv_data->tlr_snoop_check) {
4656                sas_device_priv_data->tlr_snoop_check++;
4657                if (!ioc->is_warpdrive &&
4658                    !scsih_is_raid(&scmd->device->sdev_gendev) &&
4659                    sas_is_tlr_enabled(scmd->device) &&
4660                    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4661                        sas_disable_tlr(scmd->device);
4662                        sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4663                }
4664        }
4665
4666        xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4667        scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4668        if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4669                log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4670        else
4671                log_info = 0;
4672        ioc_status &= MPI2_IOCSTATUS_MASK;
4673        scsi_status = mpi_reply->SCSIStatus;
4674
4675        if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4676            (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4677             scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4678             scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4679                ioc_status = MPI2_IOCSTATUS_SUCCESS;
4680        }
4681
4682        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4683                struct sense_info data;
4684                const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4685                    smid);
4686                u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4687                    le32_to_cpu(mpi_reply->SenseCount));
4688                memcpy(scmd->sense_buffer, sense_data, sz);
4689                _scsih_normalize_sense(scmd->sense_buffer, &data);
4690                /* failure prediction threshold exceeded */
4691                if (data.asc == 0x5D)
4692                        _scsih_smart_predicted_fault(ioc,
4693                            le16_to_cpu(mpi_reply->DevHandle));
4694                mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4695
4696                if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4697                     ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4698                     (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4699                     (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4700                        _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4701        }
4702        switch (ioc_status) {
4703        case MPI2_IOCSTATUS_BUSY:
4704        case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4705                scmd->result = SAM_STAT_BUSY;
4706                break;
4707
4708        case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4709                scmd->result = DID_NO_CONNECT << 16;
4710                break;
4711
4712        case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4713                if (sas_device_priv_data->block) {
4714                        scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4715                        goto out;
4716                }
4717                if (log_info == 0x31110630) {
4718                        if (scmd->retries > 2) {
4719                                scmd->result = DID_NO_CONNECT << 16;
4720                                scsi_device_set_state(scmd->device,
4721                                    SDEV_OFFLINE);
4722                        } else {
4723                                scmd->result = DID_SOFT_ERROR << 16;
4724                                scmd->device->expecting_cc_ua = 1;
4725                        }
4726                        break;
4727                } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4728                        scmd->result = DID_RESET << 16;
4729                        break;
4730                }
4731                scmd->result = DID_SOFT_ERROR << 16;
4732                break;
4733        case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4734        case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4735                scmd->result = DID_RESET << 16;
4736                break;
4737
4738        case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4739                if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4740                        scmd->result = DID_SOFT_ERROR << 16;
4741                else
4742                        scmd->result = (DID_OK << 16) | scsi_status;
4743                break;
4744
4745        case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4746                scmd->result = (DID_OK << 16) | scsi_status;
4747
4748                if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4749                        break;
4750
4751                if (xfer_cnt < scmd->underflow) {
4752                        if (scsi_status == SAM_STAT_BUSY)
4753                                scmd->result = SAM_STAT_BUSY;
4754                        else
4755                                scmd->result = DID_SOFT_ERROR << 16;
4756                } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4757                     MPI2_SCSI_STATE_NO_SCSI_STATUS))
4758                        scmd->result = DID_SOFT_ERROR << 16;
4759                else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4760                        scmd->result = DID_RESET << 16;
4761                else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4762                        mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4763                        mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4764                        scmd->result = (DRIVER_SENSE << 24) |
4765                            SAM_STAT_CHECK_CONDITION;
4766                        scmd->sense_buffer[0] = 0x70;
4767                        scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4768                        scmd->sense_buffer[12] = 0x20;
4769                        scmd->sense_buffer[13] = 0;
4770                }
4771                break;
4772
4773        case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4774                scsi_set_resid(scmd, 0);
4775        case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4776        case MPI2_IOCSTATUS_SUCCESS:
4777                scmd->result = (DID_OK << 16) | scsi_status;
4778                if (response_code ==
4779                    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4780                    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4781                     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4782                        scmd->result = DID_SOFT_ERROR << 16;
4783                else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4784                        scmd->result = DID_RESET << 16;
4785                break;
4786
4787        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4788        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4789        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4790                _scsih_eedp_error_handling(scmd, ioc_status);
4791                break;
4792
4793        case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4794        case MPI2_IOCSTATUS_INVALID_FUNCTION:
4795        case MPI2_IOCSTATUS_INVALID_SGL:
4796        case MPI2_IOCSTATUS_INTERNAL_ERROR:
4797        case MPI2_IOCSTATUS_INVALID_FIELD:
4798        case MPI2_IOCSTATUS_INVALID_STATE:
4799        case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4800        case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4801        case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4802        default:
4803                scmd->result = DID_SOFT_ERROR << 16;
4804                break;
4805
4806        }
4807
4808        if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4809                _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4810
4811 out:
4812
4813        scsi_dma_unmap(scmd);
4814
4815        scmd->scsi_done(scmd);
4816        return 1;
4817}
4818
4819/**
4820 * _scsih_sas_host_refresh - refreshing sas host object contents
4821 * @ioc: per adapter object
4822 * Context: user
4823 *
4824 * During port enable, fw will send topology events for every device. Its
4825 * possible that the handles may change from the previous setting, so this
4826 * code keeping handles updating if changed.
4827 *
4828 * Return nothing.
4829 */
4830static void
4831_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4832{
4833        u16 sz;
4834        u16 ioc_status;
4835        int i;
4836        Mpi2ConfigReply_t mpi_reply;
4837        Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4838        u16 attached_handle;
4839        u8 link_rate;
4840
4841        dtmprintk(ioc, pr_info(MPT3SAS_FMT
4842            "updating handles for sas_host(0x%016llx)\n",
4843            ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4844
4845        sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4846            * sizeof(Mpi2SasIOUnit0PhyData_t));
4847        sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4848        if (!sas_iounit_pg0) {
4849                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4850                    ioc->name, __FILE__, __LINE__, __func__);
4851                return;
4852        }
4853
4854        if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4855            sas_iounit_pg0, sz)) != 0)
4856                goto out;
4857        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4858        if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4859                goto out;
4860        for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4861                link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4862                if (i == 0)
4863                        ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4864                            PhyData[0].ControllerDevHandle);
4865                ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4866                attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4867                    AttachedDevHandle);
4868                if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4869                        link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4870                mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4871                    attached_handle, i, link_rate);
4872        }
4873 out:
4874        kfree(sas_iounit_pg0);
4875}
4876
4877/**
4878 * _scsih_sas_host_add - create sas host object
4879 * @ioc: per adapter object
4880 *
4881 * Creating host side data object, stored in ioc->sas_hba
4882 *
4883 * Return nothing.
4884 */
4885static void
4886_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4887{
4888        int i;
4889        Mpi2ConfigReply_t mpi_reply;
4890        Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4891        Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4892        Mpi2SasPhyPage0_t phy_pg0;
4893        Mpi2SasDevicePage0_t sas_device_pg0;
4894        Mpi2SasEnclosurePage0_t enclosure_pg0;
4895        u16 ioc_status;
4896        u16 sz;
4897        u8 device_missing_delay;
4898
4899        mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4900        if (!ioc->sas_hba.num_phys) {
4901                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4902                    ioc->name, __FILE__, __LINE__, __func__);
4903                return;
4904        }
4905
4906        /* sas_iounit page 0 */
4907        sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4908            sizeof(Mpi2SasIOUnit0PhyData_t));
4909        sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4910        if (!sas_iounit_pg0) {
4911                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4912                    ioc->name, __FILE__, __LINE__, __func__);
4913                return;
4914        }
4915        if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4916            sas_iounit_pg0, sz))) {
4917                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4918                    ioc->name, __FILE__, __LINE__, __func__);
4919                goto out;
4920        }
4921        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4922            MPI2_IOCSTATUS_MASK;
4923        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4924                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4925                    ioc->name, __FILE__, __LINE__, __func__);
4926                goto out;
4927        }
4928
4929        /* sas_iounit page 1 */
4930        sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4931            sizeof(Mpi2SasIOUnit1PhyData_t));
4932        sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4933        if (!sas_iounit_pg1) {
4934                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4935                    ioc->name, __FILE__, __LINE__, __func__);
4936                goto out;
4937        }
4938        if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4939            sas_iounit_pg1, sz))) {
4940                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4941                    ioc->name, __FILE__, __LINE__, __func__);
4942                goto out;
4943        }
4944        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4945            MPI2_IOCSTATUS_MASK;
4946        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4947                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4948                    ioc->name, __FILE__, __LINE__, __func__);
4949                goto out;
4950        }
4951
4952        ioc->io_missing_delay =
4953            sas_iounit_pg1->IODeviceMissingDelay;
4954        device_missing_delay =
4955            sas_iounit_pg1->ReportDeviceMissingDelay;
4956        if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4957                ioc->device_missing_delay = (device_missing_delay &
4958                    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4959        else
4960                ioc->device_missing_delay = device_missing_delay &
4961                    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4962
4963        ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4964        ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4965            sizeof(struct _sas_phy), GFP_KERNEL);
4966        if (!ioc->sas_hba.phy) {
4967                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4968                    ioc->name, __FILE__, __LINE__, __func__);
4969                goto out;
4970        }
4971        for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4972                if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4973                    i))) {
4974                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4975                            ioc->name, __FILE__, __LINE__, __func__);
4976                        goto out;
4977                }
4978                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4979                    MPI2_IOCSTATUS_MASK;
4980                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4981                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4982                            ioc->name, __FILE__, __LINE__, __func__);
4983                        goto out;
4984                }
4985
4986                if (i == 0)
4987                        ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4988                            PhyData[0].ControllerDevHandle);
4989                ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4990                ioc->sas_hba.phy[i].phy_id = i;
4991                mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4992                    phy_pg0, ioc->sas_hba.parent_dev);
4993        }
4994        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4995            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4996                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4997                    ioc->name, __FILE__, __LINE__, __func__);
4998                goto out;
4999        }
5000        ioc->sas_hba.enclosure_handle =
5001            le16_to_cpu(sas_device_pg0.EnclosureHandle);
5002        ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5003        pr_info(MPT3SAS_FMT
5004                "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5005                ioc->name, ioc->sas_hba.handle,
5006            (unsigned long long) ioc->sas_hba.sas_address,
5007            ioc->sas_hba.num_phys) ;
5008
5009        if (ioc->sas_hba.enclosure_handle) {
5010                if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5011                    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5012                   ioc->sas_hba.enclosure_handle)))
5013                        ioc->sas_hba.enclosure_logical_id =
5014                            le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5015        }
5016
5017 out:
5018        kfree(sas_iounit_pg1);
5019        kfree(sas_iounit_pg0);
5020}
5021
5022/**
5023 * _scsih_expander_add -  creating expander object
5024 * @ioc: per adapter object
5025 * @handle: expander handle
5026 *
5027 * Creating expander object, stored in ioc->sas_expander_list.
5028 *
5029 * Return 0 for success, else error.
5030 */
5031static int
5032_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5033{
5034        struct _sas_node *sas_expander;
5035        Mpi2ConfigReply_t mpi_reply;
5036        Mpi2ExpanderPage0_t expander_pg0;
5037        Mpi2ExpanderPage1_t expander_pg1;
5038        Mpi2SasEnclosurePage0_t enclosure_pg0;
5039        u32 ioc_status;
5040        u16 parent_handle;
5041        u64 sas_address, sas_address_parent = 0;
5042        int i;
5043        unsigned long flags;
5044        struct _sas_port *mpt3sas_port = NULL;
5045
5046        int rc = 0;
5047
5048        if (!handle)
5049                return -1;
5050
5051        if (ioc->shost_recovery || ioc->pci_error_recovery)
5052                return -1;
5053
5054        if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5055            MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5056                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5057                    ioc->name, __FILE__, __LINE__, __func__);
5058                return -1;
5059        }
5060
5061        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5062            MPI2_IOCSTATUS_MASK;
5063        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5064                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5065                    ioc->name, __FILE__, __LINE__, __func__);
5066                return -1;
5067        }
5068
5069        /* handle out of order topology events */
5070        parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5071        if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5072            != 0) {
5073                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5074                    ioc->name, __FILE__, __LINE__, __func__);
5075                return -1;
5076        }
5077        if (sas_address_parent != ioc->sas_hba.sas_address) {
5078                spin_lock_irqsave(&ioc->sas_node_lock, flags);
5079                sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5080                    sas_address_parent);
5081                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5082                if (!sas_expander) {
5083                        rc = _scsih_expander_add(ioc, parent_handle);
5084                        if (rc != 0)
5085                                return rc;
5086                }
5087        }
5088
5089        spin_lock_irqsave(&ioc->sas_node_lock, flags);
5090        sas_address = le64_to_cpu(expander_pg0.SASAddress);
5091        sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5092            sas_address);
5093        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5094
5095        if (sas_expander)
5096                return 0;
5097
5098        sas_expander = kzalloc(sizeof(struct _sas_node),
5099            GFP_KERNEL);
5100        if (!sas_expander) {
5101                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5102                    ioc->name, __FILE__, __LINE__, __func__);
5103                return -1;
5104        }
5105
5106        sas_expander->handle = handle;
5107        sas_expander->num_phys = expander_pg0.NumPhys;
5108        sas_expander->sas_address_parent = sas_address_parent;
5109        sas_expander->sas_address = sas_address;
5110
5111        pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5112            " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5113            handle, parent_handle, (unsigned long long)
5114            sas_expander->sas_address, sas_expander->num_phys);
5115
5116        if (!sas_expander->num_phys)
5117                goto out_fail;
5118        sas_expander->phy = kcalloc(sas_expander->num_phys,
5119            sizeof(struct _sas_phy), GFP_KERNEL);
5120        if (!sas_expander->phy) {
5121                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5122                    ioc->name, __FILE__, __LINE__, __func__);
5123                rc = -1;
5124                goto out_fail;
5125        }
5126
5127        INIT_LIST_HEAD(&sas_expander->sas_port_list);
5128        mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5129            sas_address_parent);
5130        if (!mpt3sas_port) {
5131                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5132                    ioc->name, __FILE__, __LINE__, __func__);
5133                rc = -1;
5134                goto out_fail;
5135        }
5136        sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5137
5138        for (i = 0 ; i < sas_expander->num_phys ; i++) {
5139                if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5140                    &expander_pg1, i, handle))) {
5141                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5142                            ioc->name, __FILE__, __LINE__, __func__);
5143                        rc = -1;
5144                        goto out_fail;
5145                }
5146                sas_expander->phy[i].handle = handle;
5147                sas_expander->phy[i].phy_id = i;
5148
5149                if ((mpt3sas_transport_add_expander_phy(ioc,
5150                    &sas_expander->phy[i], expander_pg1,
5151                    sas_expander->parent_dev))) {
5152                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5153                            ioc->name, __FILE__, __LINE__, __func__);
5154                        rc = -1;
5155                        goto out_fail;
5156                }
5157        }
5158
5159        if (sas_expander->enclosure_handle) {
5160                if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5161                    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5162                   sas_expander->enclosure_handle)))
5163                        sas_expander->enclosure_logical_id =
5164                            le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5165        }
5166
5167        _scsih_expander_node_add(ioc, sas_expander);
5168         return 0;
5169
5170 out_fail:
5171
5172        if (mpt3sas_port)
5173                mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5174                    sas_address_parent);
5175        kfree(sas_expander);
5176        return rc;
5177}
5178
5179/**
5180 * mpt3sas_expander_remove - removing expander object
5181 * @ioc: per adapter object
5182 * @sas_address: expander sas_address
5183 *
5184 * Return nothing.
5185 */
5186void
5187mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5188{
5189        struct _sas_node *sas_expander;
5190        unsigned long flags;
5191
5192        if (ioc->shost_recovery)
5193                return;
5194
5195        spin_lock_irqsave(&ioc->sas_node_lock, flags);
5196        sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5197            sas_address);
5198        if (sas_expander)
5199                list_del(&sas_expander->list);
5200        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5201        if (sas_expander)
5202                _scsih_expander_node_remove(ioc, sas_expander);
5203}
5204
5205/**
5206 * _scsih_done -  internal SCSI_IO callback handler.
5207 * @ioc: per adapter object
5208 * @smid: system request message index
5209 * @msix_index: MSIX table index supplied by the OS
5210 * @reply: reply message frame(lower 32bit addr)
5211 *
5212 * Callback handler when sending internal generated SCSI_IO.
5213 * The callback index passed is `ioc->scsih_cb_idx`
5214 *
5215 * Return 1 meaning mf should be freed from _base_interrupt
5216 *        0 means the mf is freed from this function.
5217 */
5218static u8
5219_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5220{
5221        MPI2DefaultReply_t *mpi_reply;
5222
5223        mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
5224        if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5225                return 1;
5226        if (ioc->scsih_cmds.smid != smid)
5227                return 1;
5228        ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5229        if (mpi_reply) {
5230                memcpy(ioc->scsih_cmds.reply, mpi_reply,
5231                    mpi_reply->MsgLength*4);
5232                ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5233        }
5234        ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5235        complete(&ioc->scsih_cmds.done);
5236        return 1;
5237}
5238
5239
5240
5241
5242#define MPT3_MAX_LUNS (255)
5243
5244
5245/**
5246 * _scsih_check_access_status - check access flags
5247 * @ioc: per adapter object
5248 * @sas_address: sas address
5249 * @handle: sas device handle
5250 * @access_flags: errors returned during discovery of the device
5251 *
5252 * Return 0 for success, else failure
5253 */
5254static u8
5255_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5256        u16 handle, u8 access_status)
5257{
5258        u8 rc = 1;
5259        char *desc = NULL;
5260
5261        switch (access_status) {
5262        case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5263        case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5264                rc = 0;
5265                break;
5266        case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5267                desc = "sata capability failed";
5268                break;
5269        case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5270                desc = "sata affiliation conflict";
5271                break;
5272        case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5273                desc = "route not addressable";
5274                break;
5275        case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5276                desc = "smp error not addressable";
5277                break;
5278        case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5279                desc = "device blocked";
5280                break;
5281        case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5282        case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5283        case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5284        case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5285        case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5286        case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5287        case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5288        case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5289        case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5290        case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5291        case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5292        case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5293                desc = "sata initialization failed";
5294                break;
5295        default:
5296                desc = "unknown";
5297                break;
5298        }
5299
5300        if (!rc)
5301                return 0;
5302
5303        pr_err(MPT3SAS_FMT
5304                "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5305                ioc->name, desc, (unsigned long long)sas_address, handle);
5306        return rc;
5307}
5308
5309/**
5310 * _scsih_check_device - checking device responsiveness
5311 * @ioc: per adapter object
5312 * @parent_sas_address: sas address of parent expander or sas host
5313 * @handle: attached device handle
5314 * @phy_numberv: phy number
5315 * @link_rate: new link rate
5316 *
5317 * Returns nothing.
5318 */
5319static void
5320_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5321        u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5322{
5323        Mpi2ConfigReply_t mpi_reply;
5324        Mpi2SasDevicePage0_t sas_device_pg0;
5325        struct _sas_device *sas_device;
5326        u32 ioc_status;
5327        unsigned long flags;
5328        u64 sas_address;
5329        struct scsi_target *starget;
5330        struct MPT3SAS_TARGET *sas_target_priv_data;
5331        u32 device_info;
5332
5333
5334        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5335            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5336                return;
5337
5338        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5339        if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5340                return;
5341
5342        /* wide port handling ~ we need only handle device once for the phy that
5343         * is matched in sas device page zero
5344         */
5345        if (phy_number != sas_device_pg0.PhyNum)
5346                return;
5347
5348        /* check if this is end device */
5349        device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5350        if (!(_scsih_is_end_device(device_info)))
5351                return;
5352
5353        spin_lock_irqsave(&ioc->sas_device_lock, flags);
5354        sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5355        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5356            sas_address);
5357
5358        if (!sas_device)
5359                goto out_unlock;
5360
5361        if (unlikely(sas_device->handle != handle)) {
5362                starget = sas_device->starget;
5363                sas_target_priv_data = starget->hostdata;
5364                starget_printk(KERN_INFO, starget,
5365                        "handle changed from(0x%04x) to (0x%04x)!!!\n",
5366                        sas_device->handle, handle);
5367                sas_target_priv_data->handle = handle;
5368                sas_device->handle = handle;
5369                if (sas_device_pg0.Flags &
5370                     MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5371                        sas_device->enclosure_level =
5372                                le16_to_cpu(sas_device_pg0.EnclosureLevel);
5373                        memcpy(&sas_device->connector_name[0],
5374                                &sas_device_pg0.ConnectorName[0], 4);
5375                } else {
5376                        sas_device->enclosure_level = 0;
5377                        sas_device->connector_name[0] = '\0';
5378                }
5379        }
5380
5381        /* check if device is present */
5382        if (!(le16_to_cpu(sas_device_pg0.Flags) &
5383            MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5384                pr_err(MPT3SAS_FMT
5385                        "device is not present handle(0x%04x), flags!!!\n",
5386                        ioc->name, handle);
5387                goto out_unlock;
5388        }
5389
5390        /* check if there were any issues with discovery */
5391        if (_scsih_check_access_status(ioc, sas_address, handle,
5392            sas_device_pg0.AccessStatus))
5393                goto out_unlock;
5394
5395        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5396        _scsih_ublock_io_device(ioc, sas_address);
5397
5398        if (sas_device)
5399                sas_device_put(sas_device);
5400        return;
5401
5402out_unlock:
5403        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5404        if (sas_device)
5405                sas_device_put(sas_device);
5406}
5407
5408/**
5409 * _scsih_add_device -  creating sas device object
5410 * @ioc: per adapter object
5411 * @handle: sas device handle
5412 * @phy_num: phy number end device attached to
5413 * @is_pd: is this hidden raid component
5414 *
5415 * Creating end device object, stored in ioc->sas_device_list.
5416 *
5417 * Returns 0 for success, non-zero for failure.
5418 */
5419static int
5420_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5421        u8 is_pd)
5422{
5423        Mpi2ConfigReply_t mpi_reply;
5424        Mpi2SasDevicePage0_t sas_device_pg0;
5425        Mpi2SasEnclosurePage0_t enclosure_pg0;
5426        struct _sas_device *sas_device;
5427        u32 ioc_status;
5428        u64 sas_address;
5429        u32 device_info;
5430
5431        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5432            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5433                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5434                    ioc->name, __FILE__, __LINE__, __func__);
5435                return -1;
5436        }
5437
5438        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5439            MPI2_IOCSTATUS_MASK;
5440        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5441                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5442                    ioc->name, __FILE__, __LINE__, __func__);
5443                return -1;
5444        }
5445
5446        /* check if this is end device */
5447        device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5448        if (!(_scsih_is_end_device(device_info)))
5449                return -1;
5450        sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5451
5452        /* check if device is present */
5453        if (!(le16_to_cpu(sas_device_pg0.Flags) &
5454            MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5455                pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5456                        ioc->name, handle);
5457                return -1;
5458        }
5459
5460        /* check if there were any issues with discovery */
5461        if (_scsih_check_access_status(ioc, sas_address, handle,
5462            sas_device_pg0.AccessStatus))
5463                return -1;
5464
5465        sas_device = mpt3sas_get_sdev_by_addr(ioc,
5466                                        sas_address);
5467        if (sas_device) {
5468                sas_device_put(sas_device);
5469                return -1;
5470        }
5471
5472        sas_device = kzalloc(sizeof(struct _sas_device),
5473            GFP_KERNEL);
5474        if (!sas_device) {
5475                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5476                    ioc->name, __FILE__, __LINE__, __func__);
5477                return 0;
5478        }
5479
5480        kref_init(&sas_device->refcount);
5481        sas_device->handle = handle;
5482        if (_scsih_get_sas_address(ioc,
5483            le16_to_cpu(sas_device_pg0.ParentDevHandle),
5484            &sas_device->sas_address_parent) != 0)
5485                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5486                    ioc->name, __FILE__, __LINE__, __func__);
5487        sas_device->enclosure_handle =
5488            le16_to_cpu(sas_device_pg0.EnclosureHandle);
5489        if (sas_device->enclosure_handle != 0)
5490                sas_device->slot =
5491                    le16_to_cpu(sas_device_pg0.Slot);
5492        sas_device->device_info = device_info;
5493        sas_device->sas_address = sas_address;
5494        sas_device->phy = sas_device_pg0.PhyNum;
5495        sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5496            MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5497
5498        if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5499                sas_device->enclosure_level =
5500                        le16_to_cpu(sas_device_pg0.EnclosureLevel);
5501                memcpy(&sas_device->connector_name[0],
5502                        &sas_device_pg0.ConnectorName[0], 4);
5503        } else {
5504                sas_device->enclosure_level = 0;
5505                sas_device->connector_name[0] = '\0';
5506        }
5507        /* get enclosure_logical_id */
5508        if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5509           ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5510           sas_device->enclosure_handle)))
5511                sas_device->enclosure_logical_id =
5512                    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5513
5514        /* get device name */
5515        sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5516
5517        if (ioc->wait_for_discovery_to_complete)
5518                _scsih_sas_device_init_add(ioc, sas_device);
5519        else
5520                _scsih_sas_device_add(ioc, sas_device);
5521
5522        sas_device_put(sas_device);
5523        return 0;
5524}
5525
5526/**
5527 * _scsih_remove_device -  removing sas device object
5528 * @ioc: per adapter object
5529 * @sas_device_delete: the sas_device object
5530 *
5531 * Return nothing.
5532 */
5533static void
5534_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5535        struct _sas_device *sas_device)
5536{
5537        struct MPT3SAS_TARGET *sas_target_priv_data;
5538
5539        if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5540             (sas_device->pfa_led_on)) {
5541                _scsih_turn_off_pfa_led(ioc, sas_device);
5542                sas_device->pfa_led_on = 0;
5543        }
5544        dewtprintk(ioc, pr_info(MPT3SAS_FMT
5545                "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5546                ioc->name, __func__,
5547            sas_device->handle, (unsigned long long)
5548            sas_device->sas_address));
5549        if (sas_device->enclosure_handle != 0)
5550                dewtprintk(ioc, pr_info(MPT3SAS_FMT
5551                    "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5552                    ioc->name, __func__,
5553                    (unsigned long long)sas_device->enclosure_logical_id,
5554                    sas_device->slot));
5555        if (sas_device->connector_name[0] != '\0')
5556                dewtprintk(ioc, pr_info(MPT3SAS_FMT
5557                  "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5558                  ioc->name, __func__,
5559                  sas_device->enclosure_level,
5560                  sas_device->connector_name));
5561
5562        if (sas_device->starget && sas_device->starget->hostdata) {
5563                sas_target_priv_data = sas_device->starget->hostdata;
5564                sas_target_priv_data->deleted = 1;
5565                _scsih_ublock_io_device(ioc, sas_device->sas_address);
5566                sas_target_priv_data->handle =
5567                     MPT3SAS_INVALID_DEVICE_HANDLE;
5568        }
5569
5570        if (!ioc->hide_drives)
5571                mpt3sas_transport_port_remove(ioc,
5572                    sas_device->sas_address,
5573                    sas_device->sas_address_parent);
5574
5575        pr_info(MPT3SAS_FMT
5576                "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5577                ioc->name, sas_device->handle,
5578            (unsigned long long) sas_device->sas_address);
5579        if (sas_device->enclosure_handle != 0)
5580                pr_info(MPT3SAS_FMT
5581                  "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5582                  ioc->name,
5583                  (unsigned long long)sas_device->enclosure_logical_id,
5584                  sas_device->slot);
5585        if (sas_device->connector_name[0] != '\0')
5586                pr_info(MPT3SAS_FMT
5587                  "removing enclosure level(0x%04x), connector name( %s)\n",
5588                  ioc->name, sas_device->enclosure_level,
5589                  sas_device->connector_name);
5590
5591        dewtprintk(ioc, pr_info(MPT3SAS_FMT
5592                "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5593                ioc->name, __func__,
5594                sas_device->handle, (unsigned long long)
5595                sas_device->sas_address));
5596        if (sas_device->enclosure_handle != 0)
5597                dewtprintk(ioc, pr_info(MPT3SAS_FMT
5598                    "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5599                    ioc->name, __func__,
5600                    (unsigned long long)sas_device->enclosure_logical_id,
5601                    sas_device->slot));
5602        if (sas_device->connector_name[0] != '\0')
5603                dewtprintk(ioc, pr_info(MPT3SAS_FMT
5604                    "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5605                    ioc->name, __func__, sas_device->enclosure_level,
5606                    sas_device->connector_name));
5607}
5608
5609/**
5610 * _scsih_sas_topology_change_event_debug - debug for topology event
5611 * @ioc: per adapter object
5612 * @event_data: event data payload
5613 * Context: user.
5614 */
5615static void
5616_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5617        Mpi2EventDataSasTopologyChangeList_t *event_data)
5618{
5619        int i;
5620        u16 handle;
5621        u16 reason_code;
5622        u8 phy_number;
5623        char *status_str = NULL;
5624        u8 link_rate, prev_link_rate;
5625
5626        switch (event_data->ExpStatus) {
5627        case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5628                status_str = "add";
5629                break;
5630        case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5631                status_str = "remove";
5632                break;
5633        case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5634        case 0:
5635                status_str =  "responding";
5636                break;
5637        case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5638                status_str = "remove delay";
5639                break;
5640        default:
5641                status_str = "unknown status";
5642                break;
5643        }
5644        pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5645            ioc->name, status_str);
5646        pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5647            "start_phy(%02d), count(%d)\n",
5648            le16_to_cpu(event_data->ExpanderDevHandle),
5649            le16_to_cpu(event_data->EnclosureHandle),
5650            event_data->StartPhyNum, event_data->NumEntries);
5651        for (i = 0; i < event_data->NumEntries; i++) {
5652                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5653                if (!handle)
5654                        continue;
5655                phy_number = event_data->StartPhyNum + i;
5656                reason_code = event_data->PHY[i].PhyStatus &
5657                    MPI2_EVENT_SAS_TOPO_RC_MASK;
5658                switch (reason_code) {
5659                case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5660                        status_str = "target add";
5661                        break;
5662                case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5663                        status_str = "target remove";
5664                        break;
5665                case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5666                        status_str = "delay target remove";
5667                        break;
5668                case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5669                        status_str = "link rate change";
5670                        break;
5671                case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5672                        status_str = "target responding";
5673                        break;
5674                default:
5675                        status_str = "unknown";
5676                        break;
5677                }
5678                link_rate = event_data->PHY[i].LinkRate >> 4;
5679                prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5680                pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5681                    " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5682                    handle, status_str, link_rate, prev_link_rate);
5683
5684        }
5685}
5686
5687/**
5688 * _scsih_sas_topology_change_event - handle topology changes
5689 * @ioc: per adapter object
5690 * @fw_event: The fw_event_work object
5691 * Context: user.
5692 *
5693 */
5694static int
5695_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5696        struct fw_event_work *fw_event)
5697{
5698        int i;
5699        u16 parent_handle, handle;
5700        u16 reason_code;
5701        u8 phy_number, max_phys;
5702        struct _sas_node *sas_expander;
5703        u64 sas_address;
5704        unsigned long flags;
5705        u8 link_rate, prev_link_rate;
5706        Mpi2EventDataSasTopologyChangeList_t *event_data =
5707                (Mpi2EventDataSasTopologyChangeList_t *)
5708                fw_event->event_data;
5709
5710        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5711                _scsih_sas_topology_change_event_debug(ioc, event_data);
5712
5713        if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5714                return 0;
5715
5716        if (!ioc->sas_hba.num_phys)
5717                _scsih_sas_host_add(ioc);
5718        else
5719                _scsih_sas_host_refresh(ioc);
5720
5721        if (fw_event->ignore) {
5722                dewtprintk(ioc, pr_info(MPT3SAS_FMT
5723                        "ignoring expander event\n", ioc->name));
5724                return 0;
5725        }
5726
5727        parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5728
5729        /* handle expander add */
5730        if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5731                if (_scsih_expander_add(ioc, parent_handle) != 0)
5732                        return 0;
5733
5734        spin_lock_irqsave(&ioc->sas_node_lock, flags);
5735        sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5736            parent_handle);
5737        if (sas_expander) {
5738                sas_address = sas_expander->sas_address;
5739                max_phys = sas_expander->num_phys;
5740        } else if (parent_handle < ioc->sas_hba.num_phys) {
5741                sas_address = ioc->sas_hba.sas_address;
5742                max_phys = ioc->sas_hba.num_phys;
5743        } else {
5744                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5745                return 0;
5746        }
5747        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5748
5749        /* handle siblings events */
5750        for (i = 0; i < event_data->NumEntries; i++) {
5751                if (fw_event->ignore) {
5752                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
5753                                "ignoring expander event\n", ioc->name));
5754                        return 0;
5755                }
5756                if (ioc->remove_host || ioc->pci_error_recovery)
5757                        return 0;
5758                phy_number = event_data->StartPhyNum + i;
5759                if (phy_number >= max_phys)
5760                        continue;
5761                reason_code = event_data->PHY[i].PhyStatus &
5762                    MPI2_EVENT_SAS_TOPO_RC_MASK;
5763                if ((event_data->PHY[i].PhyStatus &
5764                    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5765                    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5766                                continue;
5767                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5768                if (!handle)
5769                        continue;
5770                link_rate = event_data->PHY[i].LinkRate >> 4;
5771                prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5772                switch (reason_code) {
5773                case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5774
5775                        if (ioc->shost_recovery)
5776                                break;
5777
5778                        if (link_rate == prev_link_rate)
5779                                break;
5780
5781                        mpt3sas_transport_update_links(ioc, sas_address,
5782                            handle, phy_number, link_rate);
5783
5784                        if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5785                                break;
5786
5787                        _scsih_check_device(ioc, sas_address, handle,
5788                            phy_number, link_rate);
5789
5790
5791                case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5792
5793                        if (ioc->shost_recovery)
5794                                break;
5795
5796                        mpt3sas_transport_update_links(ioc, sas_address,
5797                            handle, phy_number, link_rate);
5798
5799                        _scsih_add_device(ioc, handle, phy_number, 0);
5800
5801                        break;
5802                case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5803
5804                        _scsih_device_remove_by_handle(ioc, handle);
5805                        break;
5806                }
5807        }
5808
5809        /* handle expander removal */
5810        if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5811            sas_expander)
5812                mpt3sas_expander_remove(ioc, sas_address);
5813
5814        return 0;
5815}
5816
5817/**
5818 * _scsih_sas_device_status_change_event_debug - debug for device event
5819 * @event_data: event data payload
5820 * Context: user.
5821 *
5822 * Return nothing.
5823 */
5824static void
5825_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5826        Mpi2EventDataSasDeviceStatusChange_t *event_data)
5827{
5828        char *reason_str = NULL;
5829
5830        switch (event_data->ReasonCode) {
5831        case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5832                reason_str = "smart data";
5833                break;
5834        case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5835                reason_str = "unsupported device discovered";
5836                break;
5837        case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5838                reason_str = "internal device reset";
5839                break;
5840        case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5841                reason_str = "internal task abort";
5842                break;
5843        case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5844                reason_str = "internal task abort set";
5845                break;
5846        case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5847                reason_str = "internal clear task set";
5848                break;
5849        case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5850                reason_str = "internal query task";
5851                break;
5852        case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5853                reason_str = "sata init failure";
5854                break;
5855        case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5856                reason_str = "internal device reset complete";
5857                break;
5858        case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5859                reason_str = "internal task abort complete";
5860                break;
5861        case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5862                reason_str = "internal async notification";
5863                break;
5864        case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5865                reason_str = "expander reduced functionality";
5866                break;
5867        case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5868                reason_str = "expander reduced functionality complete";
5869                break;
5870        default:
5871                reason_str = "unknown reason";
5872                break;
5873        }
5874        pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5875            "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5876            ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5877            (unsigned long long)le64_to_cpu(event_data->SASAddress),
5878            le16_to_cpu(event_data->TaskTag));
5879        if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5880                pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5881                    event_data->ASC, event_data->ASCQ);
5882        pr_info("\n");
5883}
5884
5885/**
5886 * _scsih_sas_device_status_change_event - handle device status change
5887 * @ioc: per adapter object
5888 * @fw_event: The fw_event_work object
5889 * Context: user.
5890 *
5891 * Return nothing.
5892 */
5893static void
5894_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5895        struct fw_event_work *fw_event)
5896{
5897        struct MPT3SAS_TARGET *target_priv_data;
5898        struct _sas_device *sas_device;
5899        u64 sas_address;
5900        unsigned long flags;
5901        Mpi2EventDataSasDeviceStatusChange_t *event_data =
5902                (Mpi2EventDataSasDeviceStatusChange_t *)
5903                fw_event->event_data;
5904
5905        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5906                _scsih_sas_device_status_change_event_debug(ioc,
5907                     event_data);
5908
5909        /* In MPI Revision K (0xC), the internal device reset complete was
5910         * implemented, so avoid setting tm_busy flag for older firmware.
5911         */
5912        if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5913                return;
5914
5915        if (event_data->ReasonCode !=
5916            MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5917           event_data->ReasonCode !=
5918            MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5919                return;
5920
5921        spin_lock_irqsave(&ioc->sas_device_lock, flags);
5922        sas_address = le64_to_cpu(event_data->SASAddress);
5923        sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5924            sas_address);
5925
5926        if (!sas_device || !sas_device->starget)
5927                goto out;
5928
5929        target_priv_data = sas_device->starget->hostdata;
5930        if (!target_priv_data)
5931                goto out;
5932
5933        if (event_data->ReasonCode ==
5934            MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5935                target_priv_data->tm_busy = 1;
5936        else
5937                target_priv_data->tm_busy = 0;
5938
5939out:
5940        if (sas_device)
5941                sas_device_put(sas_device);
5942
5943        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5944
5945}
5946
5947/**
5948 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5949 * event
5950 * @ioc: per adapter object
5951 * @event_data: event data payload
5952 * Context: user.
5953 *
5954 * Return nothing.
5955 */
5956static void
5957_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5958        Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5959{
5960        char *reason_str = NULL;
5961
5962        switch (event_data->ReasonCode) {
5963        case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5964                reason_str = "enclosure add";
5965                break;
5966        case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5967                reason_str = "enclosure remove";
5968                break;
5969        default:
5970                reason_str = "unknown reason";
5971                break;
5972        }
5973
5974        pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5975            "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5976            " number slots(%d)\n", ioc->name, reason_str,
5977            le16_to_cpu(event_data->EnclosureHandle),
5978            (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5979            le16_to_cpu(event_data->StartSlot));
5980}
5981
5982/**
5983 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5984 * @ioc: per adapter object
5985 * @fw_event: The fw_event_work object
5986 * Context: user.
5987 *
5988 * Return nothing.
5989 */
5990static void
5991_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5992        struct fw_event_work *fw_event)
5993{
5994        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5995                _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5996                     (Mpi2EventDataSasEnclDevStatusChange_t *)
5997                     fw_event->event_data);
5998}
5999
6000/**
6001 * _scsih_sas_broadcast_primitive_event - handle broadcast events
6002 * @ioc: per adapter object
6003 * @fw_event: The fw_event_work object
6004 * Context: user.
6005 *
6006 * Return nothing.
6007 */
6008static void
6009_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6010        struct fw_event_work *fw_event)
6011{
6012        struct scsi_cmnd *scmd;
6013        struct scsi_device *sdev;
6014        u16 smid, handle;
6015        u32 lun;
6016        struct MPT3SAS_DEVICE *sas_device_priv_data;
6017        u32 termination_count;
6018        u32 query_count;
6019        Mpi2SCSITaskManagementReply_t *mpi_reply;
6020        Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6021                (Mpi2EventDataSasBroadcastPrimitive_t *)
6022                fw_event->event_data;
6023        u16 ioc_status;
6024        unsigned long flags;
6025        int r;
6026        u8 max_retries = 0;
6027        u8 task_abort_retries;
6028
6029        mutex_lock(&ioc->tm_cmds.mutex);
6030        pr_info(MPT3SAS_FMT
6031                "%s: enter: phy number(%d), width(%d)\n",
6032                ioc->name, __func__, event_data->PhyNum,
6033             event_data->PortWidth);
6034
6035        _scsih_block_io_all_device(ioc);
6036
6037        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6038        mpi_reply = ioc->tm_cmds.reply;
6039 broadcast_aen_retry:
6040
6041        /* sanity checks for retrying this loop */
6042        if (max_retries++ == 5) {
6043                dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6044                    ioc->name, __func__));
6045                goto out;
6046        } else if (max_retries > 1)
6047                dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6048                    ioc->name, __func__, max_retries - 1));
6049
6050        termination_count = 0;
6051        query_count = 0;
6052        for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6053                if (ioc->shost_recovery)
6054                        goto out;
6055                scmd = _scsih_scsi_lookup_get(ioc, smid);
6056                if (!scmd)
6057                        continue;
6058                sdev = scmd->device;
6059                sas_device_priv_data = sdev->hostdata;
6060                if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6061                        continue;
6062                 /* skip hidden raid components */
6063                if (sas_device_priv_data->sas_target->flags &
6064                    MPT_TARGET_FLAGS_RAID_COMPONENT)
6065                        continue;
6066                 /* skip volumes */
6067                if (sas_device_priv_data->sas_target->flags &
6068                    MPT_TARGET_FLAGS_VOLUME)
6069                        continue;
6070
6071                handle = sas_device_priv_data->sas_target->handle;
6072                lun = sas_device_priv_data->lun;
6073                query_count++;
6074
6075                if (ioc->shost_recovery)
6076                        goto out;
6077
6078                spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6079                r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
6080                    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
6081                    TM_MUTEX_OFF);
6082                if (r == FAILED) {
6083                        sdev_printk(KERN_WARNING, sdev,
6084                            "mpt3sas_scsih_issue_tm: FAILED when sending "
6085                            "QUERY_TASK: scmd(%p)\n", scmd);
6086                        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6087                        goto broadcast_aen_retry;
6088                }
6089                ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6090                    & MPI2_IOCSTATUS_MASK;
6091                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6092                        sdev_printk(KERN_WARNING, sdev,
6093                                "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6094                                ioc_status, scmd);
6095                        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6096                        goto broadcast_aen_retry;
6097                }
6098
6099                /* see if IO is still owned by IOC and target */
6100                if (mpi_reply->ResponseCode ==
6101                     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6102                     mpi_reply->ResponseCode ==
6103                     MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6104                        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6105                        continue;
6106                }
6107                task_abort_retries = 0;
6108 tm_retry:
6109                if (task_abort_retries++ == 60) {
6110                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
6111                            "%s: ABORT_TASK: giving up\n", ioc->name,
6112                            __func__));
6113                        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6114                        goto broadcast_aen_retry;
6115                }
6116
6117                if (ioc->shost_recovery)
6118                        goto out_no_lock;
6119
6120                r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
6121                    sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
6122                    TM_MUTEX_OFF);
6123                if (r == FAILED) {
6124                        sdev_printk(KERN_WARNING, sdev,
6125                            "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6126                            "scmd(%p)\n", scmd);
6127                        goto tm_retry;
6128                }
6129
6130                if (task_abort_retries > 1)
6131                        sdev_printk(KERN_WARNING, sdev,
6132                            "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6133                            " scmd(%p)\n",
6134                            task_abort_retries - 1, scmd);
6135
6136                termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6137                spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6138        }
6139
6140        if (ioc->broadcast_aen_pending) {
6141                dewtprintk(ioc, pr_info(MPT3SAS_FMT
6142                        "%s: loop back due to pending AEN\n",
6143                        ioc->name, __func__));
6144                 ioc->broadcast_aen_pending = 0;
6145                 goto broadcast_aen_retry;
6146        }
6147
6148 out:
6149        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6150 out_no_lock:
6151
6152        dewtprintk(ioc, pr_info(MPT3SAS_FMT
6153            "%s - exit, query_count = %d termination_count = %d\n",
6154            ioc->name, __func__, query_count, termination_count));
6155
6156        ioc->broadcast_aen_busy = 0;
6157        if (!ioc->shost_recovery)
6158                _scsih_ublock_io_all_device(ioc);
6159        mutex_unlock(&ioc->tm_cmds.mutex);
6160}
6161
6162/**
6163 * _scsih_sas_discovery_event - handle discovery events
6164 * @ioc: per adapter object
6165 * @fw_event: The fw_event_work object
6166 * Context: user.
6167 *
6168 * Return nothing.
6169 */
6170static void
6171_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6172        struct fw_event_work *fw_event)
6173{
6174        Mpi2EventDataSasDiscovery_t *event_data =
6175                (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
6176
6177        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6178                pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6179                    (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6180                    "start" : "stop");
6181                if (event_data->DiscoveryStatus)
6182                        pr_info("discovery_status(0x%08x)",
6183                            le32_to_cpu(event_data->DiscoveryStatus));
6184                pr_info("\n");
6185        }
6186
6187        if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6188            !ioc->sas_hba.num_phys) {
6189                if (disable_discovery > 0 && ioc->shost_recovery) {
6190                        /* Wait for the reset to complete */
6191                        while (ioc->shost_recovery)
6192                                ssleep(1);
6193                }
6194                _scsih_sas_host_add(ioc);
6195        }
6196}
6197
6198/**
6199 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6200 * @ioc: per adapter object
6201 * @handle: device handle for physical disk
6202 * @phys_disk_num: physical disk number
6203 *
6204 * Return 0 for success, else failure.
6205 */
6206static int
6207_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6208{
6209        Mpi2RaidActionRequest_t *mpi_request;
6210        Mpi2RaidActionReply_t *mpi_reply;
6211        u16 smid;
6212        u8 issue_reset = 0;
6213        int rc = 0;
6214        u16 ioc_status;
6215        u32 log_info;
6216
6217        if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6218                return rc;
6219
6220        mutex_lock(&ioc->scsih_cmds.mutex);
6221
6222        if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6223                pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6224                    ioc->name, __func__);
6225                rc = -EAGAIN;
6226                goto out;
6227        }
6228        ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6229
6230        smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6231        if (!smid) {
6232                pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6233                    ioc->name, __func__);
6234                ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6235                rc = -EAGAIN;
6236                goto out;
6237        }
6238
6239        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6240        ioc->scsih_cmds.smid = smid;
6241        memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6242
6243        mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6244        mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6245        mpi_request->PhysDiskNum = phys_disk_num;
6246
6247        dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6248            "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6249            handle, phys_disk_num));
6250
6251        init_completion(&ioc->scsih_cmds.done);
6252        mpt3sas_base_put_smid_default(ioc, smid);
6253        wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6254
6255        if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6256                pr_err(MPT3SAS_FMT "%s: timeout\n",
6257                    ioc->name, __func__);
6258                if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6259                        issue_reset = 1;
6260                rc = -EFAULT;
6261                goto out;
6262        }
6263
6264        if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6265
6266                mpi_reply = ioc->scsih_cmds.reply;
6267                ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6268                if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6269                        log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6270                else
6271                        log_info = 0;
6272                ioc_status &= MPI2_IOCSTATUS_MASK;
6273                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6274                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
6275                            "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6276                            "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6277                            log_info));
6278                        rc = -EFAULT;
6279                } else
6280                        dewtprintk(ioc, pr_info(MPT3SAS_FMT
6281                            "IR RAID_ACTION: completed successfully\n",
6282                            ioc->name));
6283        }
6284
6285 out:
6286        ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6287        mutex_unlock(&ioc->scsih_cmds.mutex);
6288
6289        if (issue_reset)
6290                mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6291                    FORCE_BIG_HAMMER);
6292        return rc;
6293}
6294
6295/**
6296 * _scsih_reprobe_lun - reprobing lun
6297 * @sdev: scsi device struct
6298 * @no_uld_attach: sdev->no_uld_attach flag setting
6299 *
6300 **/
6301static void
6302_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6303{
6304        int rc;
6305        sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6306        sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6307            sdev->no_uld_attach ? "hidding" : "exposing");
6308        rc = scsi_device_reprobe(sdev);
6309}
6310
6311/**
6312 * _scsih_sas_volume_add - add new volume
6313 * @ioc: per adapter object
6314 * @element: IR config element data
6315 * Context: user.
6316 *
6317 * Return nothing.
6318 */
6319static void
6320_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6321        Mpi2EventIrConfigElement_t *element)
6322{
6323        struct _raid_device *raid_device;
6324        unsigned long flags;
6325        u64 wwid;
6326        u16 handle = le16_to_cpu(element->VolDevHandle);
6327        int rc;
6328
6329        mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6330        if (!wwid) {
6331                pr_err(MPT3SAS_FMT
6332                    "failure at %s:%d/%s()!\n", ioc->name,
6333                    __FILE__, __LINE__, __func__);
6334                return;
6335        }
6336
6337        spin_lock_irqsave(&ioc->raid_device_lock, flags);
6338        raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6339        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6340
6341        if (raid_device)
6342                return;
6343
6344        raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6345        if (!raid_device) {
6346                pr_err(MPT3SAS_FMT
6347                    "failure at %s:%d/%s()!\n", ioc->name,
6348                    __FILE__, __LINE__, __func__);
6349                return;
6350        }
6351
6352        raid_device->id = ioc->sas_id++;
6353        raid_device->channel = RAID_CHANNEL;
6354        raid_device->handle = handle;
6355        raid_device->wwid = wwid;
6356        _scsih_raid_device_add(ioc, raid_device);
6357        if (!ioc->wait_for_discovery_to_complete) {
6358                rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6359                    raid_device->id, 0);
6360                if (rc)
6361                        _scsih_raid_device_remove(ioc, raid_device);
6362        } else {
6363                spin_lock_irqsave(&ioc->raid_device_lock, flags);
6364                _scsih_determine_boot_device(ioc, raid_device, 1);
6365                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6366        }
6367}
6368
6369/**
6370 * _scsih_sas_volume_delete - delete volume
6371 * @ioc: per adapter object
6372 * @handle: volume device handle
6373 * Context: user.
6374 *
6375 * Return nothing.
6376 */
6377static void
6378_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6379{
6380        struct _raid_device *raid_device;
6381        unsigned long flags;
6382        struct MPT3SAS_TARGET *sas_target_priv_data;
6383        struct scsi_target *starget = NULL;
6384
6385        spin_lock_irqsave(&ioc->raid_device_lock, flags);
6386        raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6387        if (raid_device) {
6388                if (raid_device->starget) {
6389                        starget = raid_device->starget;
6390                        sas_target_priv_data = starget->hostdata;
6391                        sas_target_priv_data->deleted = 1;
6392                }
6393                pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6394                        ioc->name,  raid_device->handle,
6395                    (unsigned long long) raid_device->wwid);
6396                list_del(&raid_device->list);
6397                kfree(raid_device);
6398        }
6399        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6400        if (starget)
6401                scsi_remove_target(&starget->dev);
6402}
6403
6404/**
6405 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6406 * @ioc: per adapter object
6407 * @element: IR config element data
6408 * Context: user.
6409 *
6410 * Return nothing.
6411 */
6412static void
6413_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6414        Mpi2EventIrConfigElement_t *element)
6415{
6416        struct _sas_device *sas_device;
6417        struct scsi_target *starget = NULL;
6418        struct MPT3SAS_TARGET *sas_target_priv_data;
6419        unsigned long flags;
6420        u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6421
6422        spin_lock_irqsave(&ioc->sas_device_lock, flags);
6423        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6424        if (sas_device) {
6425                sas_device->volume_handle = 0;
6426                sas_device->volume_wwid = 0;
6427                clear_bit(handle, ioc->pd_handles);
6428                if (sas_device->starget && sas_device->starget->hostdata) {
6429                        starget = sas_device->starget;
6430                        sas_target_priv_data = starget->hostdata;
6431                        sas_target_priv_data->flags &=
6432                            ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6433                }
6434        }
6435        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6436        if (!sas_device)
6437                return;
6438
6439        /* exposing raid component */
6440        if (starget)
6441                starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6442
6443        sas_device_put(sas_device);
6444}
6445
6446/**
6447 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6448 * @ioc: per adapter object
6449 * @element: IR config element data
6450 * Context: user.
6451 *
6452 * Return nothing.
6453 */
6454static void
6455_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6456        Mpi2EventIrConfigElement_t *element)
6457{
6458        struct _sas_device *sas_device;
6459        struct scsi_target *starget = NULL;
6460        struct MPT3SAS_TARGET *sas_target_priv_data;
6461        unsigned long flags;
6462        u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6463        u16 volume_handle = 0;
6464        u64 volume_wwid = 0;
6465
6466        mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6467        if (volume_handle)
6468                mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6469                    &volume_wwid);
6470
6471        spin_lock_irqsave(&ioc->sas_device_lock, flags);
6472        sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6473        if (sas_device) {
6474                set_bit(handle, ioc->pd_handles);
6475                if (sas_device->starget && sas_device->starget->hostdata) {
6476                        starget = sas_device->starget;
6477                        sas_target_priv_data = starget->hostdata;
6478                        sas_target_priv_data->flags |=
6479                            MPT_TARGET_FLAGS_RAID_COMPONENT;
6480                        sas_device->volume_handle = volume_handle;
6481                        sas_device->volume_wwid = volume_wwid;
6482                }
6483        }
6484        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6485        if (!sas_device)
6486                return;
6487
6488        /* hiding raid component */
6489        _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6490
6491        if (starget)
6492                starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6493
6494        sas_device_put(sas_device);
6495}
6496
6497/**
6498 * _scsih_sas_pd_delete - delete pd component
6499 * @ioc: per adapter object
6500 * @element: IR config element data
6501 * Context: user.
6502 *
6503 * Return nothing.
6504 */
6505static void
6506_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6507        Mpi2EventIrConfigElement_t *element)
6508{
6509        u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6510
6511        _scsih_device_remove_by_handle(ioc, handle);
6512}
6513
6514/**
6515 * _scsih_sas_pd_add - remove pd component
6516 * @ioc: per adapter object
6517 * @element: IR config element data
6518 * Context: user.
6519 *
6520 * Return nothing.
6521 */
6522static void
6523_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6524        Mpi2EventIrConfigElement_t *element)
6525{
6526        struct _sas_device *sas_device;
6527        u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6528        Mpi2ConfigReply_t mpi_reply;
6529        Mpi2SasDevicePage0_t sas_device_pg0;
6530        u32 ioc_status;
6531        u64 sas_address;
6532        u16 parent_handle;
6533
6534        set_bit(handle, ioc->pd_handles);
6535
6536        sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6537        if (sas_device) {
6538                _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6539                sas_device_put(sas_device);
6540                return;
6541        }
6542
6543        if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6544            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6545                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6546                    ioc->name, __FILE__, __LINE__, __func__);
6547                return;
6548        }
6549
6550        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6551            MPI2_IOCSTATUS_MASK;
6552        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6553                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6554                    ioc->name, __FILE__, __LINE__, __func__);
6555                return;
6556        }
6557
6558        parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6559        if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6560                mpt3sas_transport_update_links(ioc, sas_address, handle,
6561                    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6562
6563        _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6564        _scsih_add_device(ioc, handle, 0, 1);
6565}
6566
6567/**
6568 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6569 * @ioc: per adapter object
6570 * @event_data: event data payload
6571 * Context: user.
6572 *
6573 * Return nothing.
6574 */
6575static void
6576_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6577        Mpi2EventDataIrConfigChangeList_t *event_data)
6578{
6579        Mpi2EventIrConfigElement_t *element;
6580        u8 element_type;
6581        int i;
6582        char *reason_str = NULL, *element_str = NULL;
6583
6584        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6585
6586        pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6587            ioc->name, (le32_to_cpu(event_data->Flags) &
6588            MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6589            "foreign" : "native", event_data->NumElements);
6590        for (i = 0; i < event_data->NumElements; i++, element++) {
6591                switch (element->ReasonCode) {
6592                case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6593                        reason_str = "add";
6594                        break;
6595                case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6596                        reason_str = "remove";
6597                        break;
6598                case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6599                        reason_str = "no change";
6600                        break;
6601                case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6602                        reason_str = "hide";
6603                        break;
6604                case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6605                        reason_str = "unhide";
6606                        break;
6607                case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6608                        reason_str = "volume_created";
6609                        break;
6610                case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6611                        reason_str = "volume_deleted";
6612                        break;
6613                case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6614                        reason_str = "pd_created";
6615                        break;
6616                case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6617                        reason_str = "pd_deleted";
6618                        break;
6619                default:
6620                        reason_str = "unknown reason";
6621                        break;
6622                }
6623                element_type = le16_to_cpu(element->ElementFlags) &
6624                    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6625                switch (element_type) {
6626                case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6627                        element_str = "volume";
6628                        break;
6629                case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6630                        element_str = "phys disk";
6631                        break;
6632                case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6633                        element_str = "hot spare";
6634                        break;
6635                default:
6636                        element_str = "unknown element";
6637                        break;
6638                }
6639                pr_info("\t(%s:%s), vol handle(0x%04x), " \
6640                    "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6641                    reason_str, le16_to_cpu(element->VolDevHandle),
6642                    le16_to_cpu(element->PhysDiskDevHandle),
6643                    element->PhysDiskNum);
6644        }
6645}
6646
6647/**
6648 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6649 * @ioc: per adapter object
6650 * @fw_event: The fw_event_work object
6651 * Context: user.
6652 *
6653 * Return nothing.
6654 */
6655static void
6656_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6657        struct fw_event_work *fw_event)
6658{
6659        Mpi2EventIrConfigElement_t *element;
6660        int i;
6661        u8 foreign_config;
6662        Mpi2EventDataIrConfigChangeList_t *event_data =
6663                (Mpi2EventDataIrConfigChangeList_t *)
6664                fw_event->event_data;
6665
6666        if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6667             (!ioc->hide_ir_msg))
6668                _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6669
6670        foreign_config = (le32_to_cpu(event_data->Flags) &
6671            MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6672
6673        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6674        if (ioc->shost_recovery &&
6675            ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6676                for (i = 0; i < event_data->NumElements; i++, element++) {
6677                        if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6678                                _scsih_ir_fastpath(ioc,
6679                                        le16_to_cpu(element->PhysDiskDevHandle),
6680                                        element->PhysDiskNum);
6681                }
6682                return;
6683        }
6684
6685        for (i = 0; i < event_data->NumElements; i++, element++) {
6686
6687                switch (element->ReasonCode) {
6688                case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6689                case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6690                        if (!foreign_config)
6691                                _scsih_sas_volume_add(ioc, element);
6692                        break;
6693                case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6694                case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6695                        if (!foreign_config)
6696                                _scsih_sas_volume_delete(ioc,
6697                                    le16_to_cpu(element->VolDevHandle));
6698                        break;
6699                case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6700                        if (!ioc->is_warpdrive)
6701                                _scsih_sas_pd_hide(ioc, element);
6702                        break;
6703                case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6704                        if (!ioc->is_warpdrive)
6705                                _scsih_sas_pd_expose(ioc, element);
6706                        break;
6707                case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6708                        if (!ioc->is_warpdrive)
6709                                _scsih_sas_pd_add(ioc, element);
6710                        break;
6711                case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6712                        if (!ioc->is_warpdrive)
6713                                _scsih_sas_pd_delete(ioc, element);
6714                        break;
6715                }
6716        }
6717}
6718
6719/**
6720 * _scsih_sas_ir_volume_event - IR volume event
6721 * @ioc: per adapter object
6722 * @fw_event: The fw_event_work object
6723 * Context: user.
6724 *
6725 * Return nothing.
6726 */
6727static void
6728_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6729        struct fw_event_work *fw_event)
6730{
6731        u64 wwid;
6732        unsigned long flags;
6733        struct _raid_device *raid_device;
6734        u16 handle;
6735        u32 state;
6736        int rc;
6737        Mpi2EventDataIrVolume_t *event_data =
6738                (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6739
6740        if (ioc->shost_recovery)
6741                return;
6742
6743        if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6744                return;
6745
6746        handle = le16_to_cpu(event_data->VolDevHandle);
6747        state = le32_to_cpu(event_data->NewValue);
6748        if (!ioc->hide_ir_msg)
6749                dewtprintk(ioc, pr_info(MPT3SAS_FMT
6750                    "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6751                    ioc->name, __func__,  handle,
6752                    le32_to_cpu(event_data->PreviousValue), state));
6753        switch (state) {
6754        case MPI2_RAID_VOL_STATE_MISSING:
6755        case MPI2_RAID_VOL_STATE_FAILED:
6756                _scsih_sas_volume_delete(ioc, handle);
6757                break;
6758
6759        case MPI2_RAID_VOL_STATE_ONLINE:
6760        case MPI2_RAID_VOL_STATE_DEGRADED:
6761        case MPI2_RAID_VOL_STATE_OPTIMAL:
6762
6763                spin_lock_irqsave(&ioc->raid_device_lock, flags);
6764                raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6765                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6766
6767                if (raid_device)
6768                        break;
6769
6770                mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6771                if (!wwid) {
6772                        pr_err(MPT3SAS_FMT
6773                            "failure at %s:%d/%s()!\n", ioc->name,
6774                            __FILE__, __LINE__, __func__);
6775                        break;
6776                }
6777
6778                raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6779                if (!raid_device) {
6780                        pr_err(MPT3SAS_FMT
6781                            "failure at %s:%d/%s()!\n", ioc->name,
6782                            __FILE__, __LINE__, __func__);
6783                        break;
6784                }
6785
6786                raid_device->id = ioc->sas_id++;
6787                raid_device->channel = RAID_CHANNEL;
6788                raid_device->handle = handle;
6789                raid_device->wwid = wwid;
6790                _scsih_raid_device_add(ioc, raid_device);
6791                rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6792                    raid_device->id, 0);
6793                if (rc)
6794                        _scsih_raid_device_remove(ioc, raid_device);
6795                break;
6796
6797        case MPI2_RAID_VOL_STATE_INITIALIZING:
6798        default:
6799                break;
6800        }
6801}
6802
6803/**
6804 * _scsih_sas_ir_physical_disk_event - PD event
6805 * @ioc: per adapter object
6806 * @fw_event: The fw_event_work object
6807 * Context: user.
6808 *
6809 * Return nothing.
6810 */
6811static void
6812_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6813        struct fw_event_work *fw_event)
6814{
6815        u16 handle, parent_handle;
6816        u32 state;
6817        struct _sas_device *sas_device;
6818        Mpi2ConfigReply_t mpi_reply;
6819        Mpi2SasDevicePage0_t sas_device_pg0;
6820        u32 ioc_status;
6821        Mpi2EventDataIrPhysicalDisk_t *event_data =
6822                (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6823        u64 sas_address;
6824
6825        if (ioc->shost_recovery)
6826                return;
6827
6828        if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6829                return;
6830
6831        handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6832        state = le32_to_cpu(event_data->NewValue);
6833
6834        if (!ioc->hide_ir_msg)
6835                dewtprintk(ioc, pr_info(MPT3SAS_FMT
6836                    "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6837                    ioc->name, __func__,  handle,
6838                    le32_to_cpu(event_data->PreviousValue), state));
6839
6840        switch (state) {
6841        case MPI2_RAID_PD_STATE_ONLINE:
6842        case MPI2_RAID_PD_STATE_DEGRADED:
6843        case MPI2_RAID_PD_STATE_REBUILDING:
6844        case MPI2_RAID_PD_STATE_OPTIMAL:
6845        case MPI2_RAID_PD_STATE_HOT_SPARE:
6846
6847                if (!ioc->is_warpdrive)
6848                        set_bit(handle, ioc->pd_handles);
6849
6850                sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6851                if (sas_device) {
6852                        sas_device_put(sas_device);
6853                        return;
6854                }
6855
6856                if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6857                    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6858                    handle))) {
6859                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6860                            ioc->name, __FILE__, __LINE__, __func__);
6861                        return;
6862                }
6863
6864                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6865                    MPI2_IOCSTATUS_MASK;
6866                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6867                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6868                            ioc->name, __FILE__, __LINE__, __func__);
6869                        return;
6870                }
6871
6872                parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6873                if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6874                        mpt3sas_transport_update_links(ioc, sas_address, handle,
6875                            sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6876
6877                _scsih_add_device(ioc, handle, 0, 1);
6878
6879                break;
6880
6881        case MPI2_RAID_PD_STATE_OFFLINE:
6882        case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6883        case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6884        default:
6885                break;
6886        }
6887}
6888
6889/**
6890 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6891 * @ioc: per adapter object
6892 * @event_data: event data payload
6893 * Context: user.
6894 *
6895 * Return nothing.
6896 */
6897static void
6898_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6899        Mpi2EventDataIrOperationStatus_t *event_data)
6900{
6901        char *reason_str = NULL;
6902
6903        switch (event_data->RAIDOperation) {
6904        case MPI2_EVENT_IR_RAIDOP_RESYNC:
6905                reason_str = "resync";
6906                break;
6907        case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6908                reason_str = "online capacity expansion";
6909                break;
6910        case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6911                reason_str = "consistency check";
6912                break;
6913        case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6914                reason_str = "background init";
6915                break;
6916        case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6917                reason_str = "make data consistent";
6918                break;
6919        }
6920
6921        if (!reason_str)
6922                return;
6923
6924        pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6925            "\thandle(0x%04x), percent complete(%d)\n",
6926            ioc->name, reason_str,
6927            le16_to_cpu(event_data->VolDevHandle),
6928            event_data->PercentComplete);
6929}
6930
6931/**
6932 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6933 * @ioc: per adapter object
6934 * @fw_event: The fw_event_work object
6935 * Context: user.
6936 *
6937 * Return nothing.
6938 */
6939static void
6940_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6941        struct fw_event_work *fw_event)
6942{
6943        Mpi2EventDataIrOperationStatus_t *event_data =
6944                (Mpi2EventDataIrOperationStatus_t *)
6945                fw_event->event_data;
6946        static struct _raid_device *raid_device;
6947        unsigned long flags;
6948        u16 handle;
6949
6950        if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6951            (!ioc->hide_ir_msg))
6952                _scsih_sas_ir_operation_status_event_debug(ioc,
6953                     event_data);
6954
6955        /* code added for raid transport support */
6956        if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6957
6958                spin_lock_irqsave(&ioc->raid_device_lock, flags);
6959                handle = le16_to_cpu(event_data->VolDevHandle);
6960                raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6961                if (raid_device)
6962                        raid_device->percent_complete =
6963                            event_data->PercentComplete;
6964                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6965        }
6966}
6967
6968/**
6969 * _scsih_prep_device_scan - initialize parameters prior to device scan
6970 * @ioc: per adapter object
6971 *
6972 * Set the deleted flag prior to device scan.  If the device is found during
6973 * the scan, then we clear the deleted flag.
6974 */
6975static void
6976_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6977{
6978        struct MPT3SAS_DEVICE *sas_device_priv_data;
6979        struct scsi_device *sdev;
6980
6981        shost_for_each_device(sdev, ioc->shost) {
6982                sas_device_priv_data = sdev->hostdata;
6983                if (sas_device_priv_data && sas_device_priv_data->sas_target)
6984                        sas_device_priv_data->sas_target->deleted = 1;
6985        }
6986}
6987
6988/**
6989 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6990 * @ioc: per adapter object
6991 * @sas_device_pg0: SAS Device page 0
6992 *
6993 * After host reset, find out whether devices are still responding.
6994 * Used in _scsih_remove_unresponsive_sas_devices.
6995 *
6996 * Return nothing.
6997 */
6998static void
6999_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7000Mpi2SasDevicePage0_t *sas_device_pg0)
7001{
7002        struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7003        struct scsi_target *starget;
7004        struct _sas_device *sas_device;
7005        unsigned long flags;
7006
7007        spin_lock_irqsave(&ioc->sas_device_lock, flags);
7008        list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
7009                if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7010                        (sas_device->slot == sas_device_pg0->Slot)) {
7011                        sas_device->responding = 1;
7012                        starget = sas_device->starget;
7013                        if (starget && starget->hostdata) {
7014                                sas_target_priv_data = starget->hostdata;
7015                                sas_target_priv_data->tm_busy = 0;
7016                                sas_target_priv_data->deleted = 0;
7017                        } else
7018                                sas_target_priv_data = NULL;
7019                        if (starget) {
7020                                starget_printk(KERN_INFO, starget,
7021                                    "handle(0x%04x), sas_addr(0x%016llx)\n",
7022                                    sas_device_pg0->DevHandle,
7023                                    (unsigned long long)
7024                                    sas_device->sas_address);
7025
7026                                if (sas_device->enclosure_handle != 0)
7027                                        starget_printk(KERN_INFO, starget,
7028                                         "enclosure logical id(0x%016llx),"
7029                                         " slot(%d)\n",
7030                                         (unsigned long long)
7031                                         sas_device->enclosure_logical_id,
7032                                         sas_device->slot);
7033                        }
7034                        if (sas_device_pg0->Flags &
7035                              MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7036                                sas_device->enclosure_level =
7037                                   le16_to_cpu(sas_device_pg0->EnclosureLevel);
7038                                memcpy(&sas_device->connector_name[0],
7039                                        &sas_device_pg0->ConnectorName[0], 4);
7040                        } else {
7041                                sas_device->enclosure_level = 0;
7042                                sas_device->connector_name[0] = '\0';
7043                        }
7044
7045                        if (sas_device->handle == sas_device_pg0->DevHandle)
7046                                goto out;
7047                        pr_info("\thandle changed from(0x%04x)!!!\n",
7048                            sas_device->handle);
7049                        sas_device->handle = sas_device_pg0->DevHandle;
7050                        if (sas_target_priv_data)
7051                                sas_target_priv_data->handle =
7052                                        sas_device_pg0->DevHandle;
7053                        goto out;
7054                }
7055        }
7056 out:
7057        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7058}
7059
7060/**
7061 * _scsih_search_responding_sas_devices -
7062 * @ioc: per adapter object
7063 *
7064 * After host reset, find out whether devices are still responding.
7065 * If not remove.
7066 *
7067 * Return nothing.
7068 */
7069static void
7070_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7071{
7072        Mpi2SasDevicePage0_t sas_device_pg0;
7073        Mpi2ConfigReply_t mpi_reply;
7074        u16 ioc_status;
7075        u16 handle;
7076        u32 device_info;
7077
7078        pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7079
7080        if (list_empty(&ioc->sas_device_list))
7081                goto out;
7082
7083        handle = 0xFFFF;
7084        while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7085            &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7086            handle))) {
7087                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7088                    MPI2_IOCSTATUS_MASK;
7089                if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7090                        break;
7091                handle = sas_device_pg0.DevHandle =
7092                                le16_to_cpu(sas_device_pg0.DevHandle);
7093                device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7094                if (!(_scsih_is_end_device(device_info)))
7095                        continue;
7096                sas_device_pg0.SASAddress =
7097                                le64_to_cpu(sas_device_pg0.SASAddress);
7098                sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7099                _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
7100        }
7101
7102 out:
7103        pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7104            ioc->name);
7105}
7106
7107/**
7108 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7109 * @ioc: per adapter object
7110 * @wwid: world wide identifier for raid volume
7111 * @handle: device handle
7112 *
7113 * After host reset, find out whether devices are still responding.
7114 * Used in _scsih_remove_unresponsive_raid_devices.
7115 *
7116 * Return nothing.
7117 */
7118static void
7119_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7120        u16 handle)
7121{
7122        struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7123        struct scsi_target *starget;
7124        struct _raid_device *raid_device;
7125        unsigned long flags;
7126
7127        spin_lock_irqsave(&ioc->raid_device_lock, flags);
7128        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7129                if (raid_device->wwid == wwid && raid_device->starget) {
7130                        starget = raid_device->starget;
7131                        if (starget && starget->hostdata) {
7132                                sas_target_priv_data = starget->hostdata;
7133                                sas_target_priv_data->deleted = 0;
7134                        } else
7135                                sas_target_priv_data = NULL;
7136                        raid_device->responding = 1;
7137                        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7138                        starget_printk(KERN_INFO, raid_device->starget,
7139                            "handle(0x%04x), wwid(0x%016llx)\n", handle,
7140                            (unsigned long long)raid_device->wwid);
7141
7142                        /*
7143                         * WARPDRIVE: The handles of the PDs might have changed
7144                         * across the host reset so re-initialize the
7145                         * required data for Direct IO
7146                         */
7147                        mpt3sas_init_warpdrive_properties(ioc, raid_device);
7148                        spin_lock_irqsave(&ioc->raid_device_lock, flags);
7149                        if (raid_device->handle == handle) {
7150                                spin_unlock_irqrestore(&ioc->raid_device_lock,
7151                                    flags);
7152                                return;
7153                        }
7154                        pr_info("\thandle changed from(0x%04x)!!!\n",
7155                            raid_device->handle);
7156                        raid_device->handle = handle;
7157                        if (sas_target_priv_data)
7158                                sas_target_priv_data->handle = handle;
7159                        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7160                        return;
7161                }
7162        }
7163        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7164}
7165
7166/**
7167 * _scsih_search_responding_raid_devices -
7168 * @ioc: per adapter object
7169 *
7170 * After host reset, find out whether devices are still responding.
7171 * If not remove.
7172 *
7173 * Return nothing.
7174 */
7175static void
7176_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7177{
7178        Mpi2RaidVolPage1_t volume_pg1;
7179        Mpi2RaidVolPage0_t volume_pg0;
7180        Mpi2RaidPhysDiskPage0_t pd_pg0;
7181        Mpi2ConfigReply_t mpi_reply;
7182        u16 ioc_status;
7183        u16 handle;
7184        u8 phys_disk_num;
7185
7186        if (!ioc->ir_firmware)
7187                return;
7188
7189        pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7190            ioc->name);
7191
7192        if (list_empty(&ioc->raid_device_list))
7193                goto out;
7194
7195        handle = 0xFFFF;
7196        while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7197            &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7198                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7199                    MPI2_IOCSTATUS_MASK;
7200                if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7201                        break;
7202                handle = le16_to_cpu(volume_pg1.DevHandle);
7203
7204                if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7205                    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7206                     sizeof(Mpi2RaidVolPage0_t)))
7207                        continue;
7208
7209                if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7210                    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7211                    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7212                        _scsih_mark_responding_raid_device(ioc,
7213                            le64_to_cpu(volume_pg1.WWID), handle);
7214        }
7215
7216        /* refresh the pd_handles */
7217        if (!ioc->is_warpdrive) {
7218                phys_disk_num = 0xFF;
7219                memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7220                while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7221                    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7222                    phys_disk_num))) {
7223                        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7224                            MPI2_IOCSTATUS_MASK;
7225                        if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7226                                break;
7227                        phys_disk_num = pd_pg0.PhysDiskNum;
7228                        handle = le16_to_cpu(pd_pg0.DevHandle);
7229                        set_bit(handle, ioc->pd_handles);
7230                }
7231        }
7232 out:
7233        pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7234                ioc->name);
7235}
7236
7237/**
7238 * _scsih_mark_responding_expander - mark a expander as responding
7239 * @ioc: per adapter object
7240 * @sas_address: sas address
7241 * @handle:
7242 *
7243 * After host reset, find out whether devices are still responding.
7244 * Used in _scsih_remove_unresponsive_expanders.
7245 *
7246 * Return nothing.
7247 */
7248static void
7249_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7250        u16 handle)
7251{
7252        struct _sas_node *sas_expander;
7253        unsigned long flags;
7254        int i;
7255
7256        spin_lock_irqsave(&ioc->sas_node_lock, flags);
7257        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7258                if (sas_expander->sas_address != sas_address)
7259                        continue;
7260                sas_expander->responding = 1;
7261                if (sas_expander->handle == handle)
7262                        goto out;
7263                pr_info("\texpander(0x%016llx): handle changed" \
7264                    " from(0x%04x) to (0x%04x)!!!\n",
7265                    (unsigned long long)sas_expander->sas_address,
7266                    sas_expander->handle, handle);
7267                sas_expander->handle = handle;
7268                for (i = 0 ; i < sas_expander->num_phys ; i++)
7269                        sas_expander->phy[i].handle = handle;
7270                goto out;
7271        }
7272 out:
7273        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7274}
7275
7276/**
7277 * _scsih_search_responding_expanders -
7278 * @ioc: per adapter object
7279 *
7280 * After host reset, find out whether devices are still responding.
7281 * If not remove.
7282 *
7283 * Return nothing.
7284 */
7285static void
7286_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7287{
7288        Mpi2ExpanderPage0_t expander_pg0;
7289        Mpi2ConfigReply_t mpi_reply;
7290        u16 ioc_status;
7291        u64 sas_address;
7292        u16 handle;
7293
7294        pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7295
7296        if (list_empty(&ioc->sas_expander_list))
7297                goto out;
7298
7299        handle = 0xFFFF;
7300        while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7301            MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7302
7303                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7304                    MPI2_IOCSTATUS_MASK;
7305                if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7306                        break;
7307
7308                handle = le16_to_cpu(expander_pg0.DevHandle);
7309                sas_address = le64_to_cpu(expander_pg0.SASAddress);
7310                pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7311                        handle,
7312                    (unsigned long long)sas_address);
7313                _scsih_mark_responding_expander(ioc, sas_address, handle);
7314        }
7315
7316 out:
7317        pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7318}
7319
7320/**
7321 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7322 * @ioc: per adapter object
7323 *
7324 * Return nothing.
7325 */
7326static void
7327_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7328{
7329        struct _sas_device *sas_device, *sas_device_next;
7330        struct _sas_node *sas_expander, *sas_expander_next;
7331        struct _raid_device *raid_device, *raid_device_next;
7332        struct list_head tmp_list;
7333        unsigned long flags;
7334        LIST_HEAD(head);
7335
7336        pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7337            ioc->name);
7338
7339        /* removing unresponding end devices */
7340        pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7341            ioc->name);
7342        /*
7343         * Iterate, pulling off devices marked as non-responding. We become the
7344         * owner for the reference the list had on any object we prune.
7345         */
7346        spin_lock_irqsave(&ioc->sas_device_lock, flags);
7347        list_for_each_entry_safe(sas_device, sas_device_next,
7348            &ioc->sas_device_list, list) {
7349                if (!sas_device->responding)
7350                        list_move_tail(&sas_device->list, &head);
7351                else
7352                        sas_device->responding = 0;
7353        }
7354        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7355
7356        /*
7357         * Now, uninitialize and remove the unresponding devices we pruned.
7358         */
7359        list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7360                _scsih_remove_device(ioc, sas_device);
7361                list_del_init(&sas_device->list);
7362                sas_device_put(sas_device);
7363        }
7364
7365        /* removing unresponding volumes */
7366        if (ioc->ir_firmware) {
7367                pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7368                        ioc->name);
7369                list_for_each_entry_safe(raid_device, raid_device_next,
7370                    &ioc->raid_device_list, list) {
7371                        if (!raid_device->responding)
7372                                _scsih_sas_volume_delete(ioc,
7373                                    raid_device->handle);
7374                        else
7375                                raid_device->responding = 0;
7376                }
7377        }
7378
7379        /* removing unresponding expanders */
7380        pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7381            ioc->name);
7382        spin_lock_irqsave(&ioc->sas_node_lock, flags);
7383        INIT_LIST_HEAD(&tmp_list);
7384        list_for_each_entry_safe(sas_expander, sas_expander_next,
7385            &ioc->sas_expander_list, list) {
7386                if (!sas_expander->responding)
7387                        list_move_tail(&sas_expander->list, &tmp_list);
7388                else
7389                        sas_expander->responding = 0;
7390        }
7391        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7392        list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7393            list) {
7394                list_del(&sas_expander->list);
7395                _scsih_expander_node_remove(ioc, sas_expander);
7396        }
7397
7398        pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7399            ioc->name);
7400
7401        /* unblock devices */
7402        _scsih_ublock_io_all_device(ioc);
7403}
7404
7405static void
7406_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7407        struct _sas_node *sas_expander, u16 handle)
7408{
7409        Mpi2ExpanderPage1_t expander_pg1;
7410        Mpi2ConfigReply_t mpi_reply;
7411        int i;
7412
7413        for (i = 0 ; i < sas_expander->num_phys ; i++) {
7414                if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7415                    &expander_pg1, i, handle))) {
7416                        pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7417                            ioc->name, __FILE__, __LINE__, __func__);
7418                        return;
7419                }
7420
7421                mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7422                    le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7423                    expander_pg1.NegotiatedLinkRate >> 4);
7424        }
7425}
7426
7427/**
7428 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7429 * @ioc: per adapter object
7430 *
7431 * Return nothing.
7432 */
7433static void
7434_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7435{
7436        Mpi2ExpanderPage0_t expander_pg0;
7437        Mpi2SasDevicePage0_t sas_device_pg0;
7438        Mpi2RaidVolPage1_t volume_pg1;
7439        Mpi2RaidVolPage0_t volume_pg0;
7440        Mpi2RaidPhysDiskPage0_t pd_pg0;
7441        Mpi2EventIrConfigElement_t element;
7442        Mpi2ConfigReply_t mpi_reply;
7443        u8 phys_disk_num;
7444        u16 ioc_status;
7445        u16 handle, parent_handle;
7446        u64 sas_address;
7447        struct _sas_device *sas_device;
7448        struct _sas_node *expander_device;
7449        static struct _raid_device *raid_device;
7450        u8 retry_count;
7451        unsigned long flags;
7452
7453        pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7454
7455        _scsih_sas_host_refresh(ioc);
7456
7457        pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7458
7459        /* expanders */
7460        handle = 0xFFFF;
7461        while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7462            MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7463                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7464                    MPI2_IOCSTATUS_MASK;
7465                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7466                        pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7467                            "ioc_status(0x%04x), loginfo(0x%08x)\n",
7468                            ioc->name, ioc_status,
7469                            le32_to_cpu(mpi_reply.IOCLogInfo));
7470                        break;
7471                }
7472                handle = le16_to_cpu(expander_pg0.DevHandle);
7473                spin_lock_irqsave(&ioc->sas_node_lock, flags);
7474                expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7475                    ioc, le64_to_cpu(expander_pg0.SASAddress));
7476                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7477                if (expander_device)
7478                        _scsih_refresh_expander_links(ioc, expander_device,
7479                            handle);
7480                else {
7481                        pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7482                            "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7483                            handle, (unsigned long long)
7484                            le64_to_cpu(expander_pg0.SASAddress));
7485                        _scsih_expander_add(ioc, handle);
7486                        pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7487                            "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7488                            handle, (unsigned long long)
7489                            le64_to_cpu(expander_pg0.SASAddress));
7490                }
7491        }
7492
7493        pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7494            ioc->name);
7495
7496        if (!ioc->ir_firmware)
7497                goto skip_to_sas;
7498
7499        pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7500
7501        /* phys disk */
7502        phys_disk_num = 0xFF;
7503        while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7504            &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7505            phys_disk_num))) {
7506                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7507                    MPI2_IOCSTATUS_MASK;
7508                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7509                        pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7510                            "ioc_status(0x%04x), loginfo(0x%08x)\n",
7511                            ioc->name, ioc_status,
7512                            le32_to_cpu(mpi_reply.IOCLogInfo));
7513                        break;
7514                }
7515                phys_disk_num = pd_pg0.PhysDiskNum;
7516                handle = le16_to_cpu(pd_pg0.DevHandle);
7517                sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7518                if (sas_device) {
7519                        sas_device_put(sas_device);
7520                        continue;
7521                }
7522                if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7523                    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7524                    handle) != 0)
7525                        continue;
7526                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7527                    MPI2_IOCSTATUS_MASK;
7528                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7529                        pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7530                            "ioc_status(0x%04x), loginfo(0x%08x)\n",
7531                            ioc->name, ioc_status,
7532                            le32_to_cpu(mpi_reply.IOCLogInfo));
7533                        break;
7534                }
7535                parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7536                if (!_scsih_get_sas_address(ioc, parent_handle,
7537                    &sas_address)) {
7538                        pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7539                            " handle (0x%04x), sas_addr(0x%016llx)\n",
7540                            ioc->name, handle, (unsigned long long)
7541                            le64_to_cpu(sas_device_pg0.SASAddress));
7542                        mpt3sas_transport_update_links(ioc, sas_address,
7543                            handle, sas_device_pg0.PhyNum,
7544                            MPI2_SAS_NEG_LINK_RATE_1_5);
7545                        set_bit(handle, ioc->pd_handles);
7546                        retry_count = 0;
7547                        /* This will retry adding the end device.
7548                         * _scsih_add_device() will decide on retries and
7549                         * return "1" when it should be retried
7550                         */
7551                        while (_scsih_add_device(ioc, handle, retry_count++,
7552                            1)) {
7553                                ssleep(1);
7554                        }
7555                        pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7556                            " handle (0x%04x), sas_addr(0x%016llx)\n",
7557                            ioc->name, handle, (unsigned long long)
7558                            le64_to_cpu(sas_device_pg0.SASAddress));
7559                }
7560        }
7561
7562        pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7563            ioc->name);
7564
7565        pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7566
7567        /* volumes */
7568        handle = 0xFFFF;
7569        while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7570            &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7571                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7572                    MPI2_IOCSTATUS_MASK;
7573                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7574                        pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7575                            "ioc_status(0x%04x), loginfo(0x%08x)\n",
7576                            ioc->name, ioc_status,
7577                            le32_to_cpu(mpi_reply.IOCLogInfo));
7578                        break;
7579                }
7580                handle = le16_to_cpu(volume_pg1.DevHandle);
7581                spin_lock_irqsave(&ioc->raid_device_lock, flags);
7582                raid_device = _scsih_raid_device_find_by_wwid(ioc,
7583                    le64_to_cpu(volume_pg1.WWID));
7584                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7585                if (raid_device)
7586                        continue;
7587                if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7588                    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7589                     sizeof(Mpi2RaidVolPage0_t)))
7590                        continue;
7591                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7592                    MPI2_IOCSTATUS_MASK;
7593                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7594                        pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7595                            "ioc_status(0x%04x), loginfo(0x%08x)\n",
7596                            ioc->name, ioc_status,
7597                            le32_to_cpu(mpi_reply.IOCLogInfo));
7598                        break;
7599                }
7600                if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7601                    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7602                    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7603                        memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7604                        element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7605                        element.VolDevHandle = volume_pg1.DevHandle;
7606                        pr_info(MPT3SAS_FMT
7607                                "\tBEFORE adding volume: handle (0x%04x)\n",
7608                                ioc->name, volume_pg1.DevHandle);
7609                        _scsih_sas_volume_add(ioc, &element);
7610                        pr_info(MPT3SAS_FMT
7611                                "\tAFTER adding volume: handle (0x%04x)\n",
7612                                ioc->name, volume_pg1.DevHandle);
7613                }
7614        }
7615
7616        pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7617            ioc->name);
7618
7619 skip_to_sas:
7620
7621        pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7622            ioc->name);
7623
7624        /* sas devices */
7625        handle = 0xFFFF;
7626        while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7627            &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7628            handle))) {
7629                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7630                    MPI2_IOCSTATUS_MASK;
7631                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7632                        pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7633                            " ioc_status(0x%04x), loginfo(0x%08x)\n",
7634                            ioc->name, ioc_status,
7635                            le32_to_cpu(mpi_reply.IOCLogInfo));
7636                        break;
7637                }
7638                handle = le16_to_cpu(sas_device_pg0.DevHandle);
7639                if (!(_scsih_is_end_device(
7640                    le32_to_cpu(sas_device_pg0.DeviceInfo))))
7641                        continue;
7642                sas_device = mpt3sas_get_sdev_by_addr(ioc,
7643                    le64_to_cpu(sas_device_pg0.SASAddress));
7644                if (sas_device) {
7645                        sas_device_put(sas_device);
7646                        continue;
7647                }
7648                parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7649                if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7650                        pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7651                            "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7652                            handle, (unsigned long long)
7653                            le64_to_cpu(sas_device_pg0.SASAddress));
7654                        mpt3sas_transport_update_links(ioc, sas_address, handle,
7655                            sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7656                        retry_count = 0;
7657                        /* This will retry adding the end device.
7658                         * _scsih_add_device() will decide on retries and
7659                         * return "1" when it should be retried
7660                         */
7661                        while (_scsih_add_device(ioc, handle, retry_count++,
7662                            0)) {
7663                                ssleep(1);
7664                        }
7665                        pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7666                            "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7667                            handle, (unsigned long long)
7668                            le64_to_cpu(sas_device_pg0.SASAddress));
7669                }
7670        }
7671        pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7672            ioc->name);
7673
7674        pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7675}
7676/**
7677 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7678 * @ioc: per adapter object
7679 * @reset_phase: phase
7680 *
7681 * The handler for doing any required cleanup or initialization.
7682 *
7683 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7684 * MPT3_IOC_DONE_RESET
7685 *
7686 * Return nothing.
7687 */
7688void
7689mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7690{
7691        switch (reset_phase) {
7692        case MPT3_IOC_PRE_RESET:
7693                dtmprintk(ioc, pr_info(MPT3SAS_FMT
7694                        "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7695                break;
7696        case MPT3_IOC_AFTER_RESET:
7697                dtmprintk(ioc, pr_info(MPT3SAS_FMT
7698                        "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7699                if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7700                        ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7701                        mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7702                        complete(&ioc->scsih_cmds.done);
7703                }
7704                if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7705                        ioc->tm_cmds.status |= MPT3_CMD_RESET;
7706                        mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7707                        complete(&ioc->tm_cmds.done);
7708                }
7709
7710                _scsih_fw_event_cleanup_queue(ioc);
7711                _scsih_flush_running_cmds(ioc);
7712                break;
7713        case MPT3_IOC_DONE_RESET:
7714                dtmprintk(ioc, pr_info(MPT3SAS_FMT
7715                        "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7716                if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7717                    !ioc->sas_hba.num_phys)) {
7718                        _scsih_prep_device_scan(ioc);
7719                        _scsih_search_responding_sas_devices(ioc);
7720                        _scsih_search_responding_raid_devices(ioc);
7721                        _scsih_search_responding_expanders(ioc);
7722                        _scsih_error_recovery_delete_devices(ioc);
7723                }
7724                break;
7725        }
7726}
7727
7728/**
7729 * _mpt3sas_fw_work - delayed task for processing firmware events
7730 * @ioc: per adapter object
7731 * @fw_event: The fw_event_work object
7732 * Context: user.
7733 *
7734 * Return nothing.
7735 */
7736static void
7737_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7738{
7739        _scsih_fw_event_del_from_list(ioc, fw_event);
7740
7741        /* the queue is being flushed so ignore this event */
7742        if (ioc->remove_host || ioc->pci_error_recovery) {
7743                fw_event_work_put(fw_event);
7744                return;
7745        }
7746
7747        switch (fw_event->event) {
7748        case MPT3SAS_PROCESS_TRIGGER_DIAG:
7749                mpt3sas_process_trigger_data(ioc,
7750                        (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7751                        fw_event->event_data);
7752                break;
7753        case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7754                while (scsi_host_in_recovery(ioc->shost) ||
7755                                         ioc->shost_recovery) {
7756                        /*
7757                         * If we're unloading, bail. Otherwise, this can become
7758                         * an infinite loop.
7759                         */
7760                        if (ioc->remove_host)
7761                                goto out;
7762                        ssleep(1);
7763                }
7764                _scsih_remove_unresponding_sas_devices(ioc);
7765                _scsih_scan_for_devices_after_reset(ioc);
7766                break;
7767        case MPT3SAS_PORT_ENABLE_COMPLETE:
7768                ioc->start_scan = 0;
7769        if (missing_delay[0] != -1 && missing_delay[1] != -1)
7770                        mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7771                            missing_delay[1]);
7772                dewtprintk(ioc, pr_info(MPT3SAS_FMT
7773                        "port enable: complete from worker thread\n",
7774                        ioc->name));
7775                break;
7776        case MPT3SAS_TURN_ON_PFA_LED:
7777                _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7778                break;
7779        case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7780                _scsih_sas_topology_change_event(ioc, fw_event);
7781                break;
7782        case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7783                _scsih_sas_device_status_change_event(ioc, fw_event);
7784                break;
7785        case MPI2_EVENT_SAS_DISCOVERY:
7786                _scsih_sas_discovery_event(ioc, fw_event);
7787                break;
7788        case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7789                _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7790                break;
7791        case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7792                _scsih_sas_enclosure_dev_status_change_event(ioc,
7793                    fw_event);
7794                break;
7795        case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7796                _scsih_sas_ir_config_change_event(ioc, fw_event);
7797                break;
7798        case MPI2_EVENT_IR_VOLUME:
7799                _scsih_sas_ir_volume_event(ioc, fw_event);
7800                break;
7801        case MPI2_EVENT_IR_PHYSICAL_DISK:
7802                _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7803                break;
7804        case MPI2_EVENT_IR_OPERATION_STATUS:
7805                _scsih_sas_ir_operation_status_event(ioc, fw_event);
7806                break;
7807        }
7808out:
7809        fw_event_work_put(fw_event);
7810}
7811
7812/**
7813 * _firmware_event_work
7814 * @ioc: per adapter object
7815 * @work: The fw_event_work object
7816 * Context: user.
7817 *
7818 * wrappers for the work thread handling firmware events
7819 *
7820 * Return nothing.
7821 */
7822
7823static void
7824_firmware_event_work(struct work_struct *work)
7825{
7826        struct fw_event_work *fw_event = container_of(work,
7827            struct fw_event_work, work);
7828
7829        _mpt3sas_fw_work(fw_event->ioc, fw_event);
7830}
7831
7832/**
7833 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7834 * @ioc: per adapter object
7835 * @msix_index: MSIX table index supplied by the OS
7836 * @reply: reply message frame(lower 32bit addr)
7837 * Context: interrupt.
7838 *
7839 * This function merely adds a new work task into ioc->firmware_event_thread.
7840 * The tasks are worked from _firmware_event_work in user context.
7841 *
7842 * Return 1 meaning mf should be freed from _base_interrupt
7843 *        0 means the mf is freed from this function.
7844 */
7845u8
7846mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7847        u32 reply)
7848{
7849        struct fw_event_work *fw_event;
7850        Mpi2EventNotificationReply_t *mpi_reply;
7851        u16 event;
7852        u16 sz;
7853
7854        /* events turned off due to host reset or driver unloading */
7855        if (ioc->remove_host || ioc->pci_error_recovery)
7856                return 1;
7857
7858        mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7859
7860        if (unlikely(!mpi_reply)) {
7861                pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7862                    ioc->name, __FILE__, __LINE__, __func__);
7863                return 1;
7864        }
7865
7866        event = le16_to_cpu(mpi_reply->Event);
7867
7868        if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7869                mpt3sas_trigger_event(ioc, event, 0);
7870
7871        switch (event) {
7872        /* handle these */
7873        case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7874        {
7875                Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7876                    (Mpi2EventDataSasBroadcastPrimitive_t *)
7877                    mpi_reply->EventData;
7878
7879                if (baen_data->Primitive !=
7880                    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7881                        return 1;
7882
7883                if (ioc->broadcast_aen_busy) {
7884                        ioc->broadcast_aen_pending++;
7885                        return 1;
7886                } else
7887                        ioc->broadcast_aen_busy = 1;
7888                break;
7889        }
7890
7891        case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7892                _scsih_check_topo_delete_events(ioc,
7893                    (Mpi2EventDataSasTopologyChangeList_t *)
7894                    mpi_reply->EventData);
7895                break;
7896        case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7897                _scsih_check_ir_config_unhide_events(ioc,
7898                    (Mpi2EventDataIrConfigChangeList_t *)
7899                    mpi_reply->EventData);
7900                break;
7901        case MPI2_EVENT_IR_VOLUME:
7902                _scsih_check_volume_delete_events(ioc,
7903                    (Mpi2EventDataIrVolume_t *)
7904                    mpi_reply->EventData);
7905                break;
7906        case MPI2_EVENT_LOG_ENTRY_ADDED:
7907        {
7908                Mpi2EventDataLogEntryAdded_t *log_entry;
7909                u32 *log_code;
7910
7911                if (!ioc->is_warpdrive)
7912                        break;
7913
7914                log_entry = (Mpi2EventDataLogEntryAdded_t *)
7915                    mpi_reply->EventData;
7916                log_code = (u32 *)log_entry->LogData;
7917
7918                if (le16_to_cpu(log_entry->LogEntryQualifier)
7919                    != MPT2_WARPDRIVE_LOGENTRY)
7920                        break;
7921
7922                switch (le32_to_cpu(*log_code)) {
7923                case MPT2_WARPDRIVE_LC_SSDT:
7924                        pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7925                            "IO Throttling has occurred in the WarpDrive "
7926                            "subsystem. Check WarpDrive documentation for "
7927                            "additional details.\n", ioc->name);
7928                        break;
7929                case MPT2_WARPDRIVE_LC_SSDLW:
7930                        pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7931                            "Program/Erase Cycles for the WarpDrive subsystem "
7932                            "in degraded range. Check WarpDrive documentation "
7933                            "for additional details.\n", ioc->name);
7934                        break;
7935                case MPT2_WARPDRIVE_LC_SSDLF:
7936                        pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7937                            "There are no Program/Erase Cycles for the "
7938                            "WarpDrive subsystem. The storage device will be "
7939                            "in read-only mode. Check WarpDrive documentation "
7940                            "for additional details.\n", ioc->name);
7941                        break;
7942                case MPT2_WARPDRIVE_LC_BRMF:
7943                        pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7944                            "The Backup Rail Monitor has failed on the "
7945                            "WarpDrive subsystem. Check WarpDrive "
7946                            "documentation for additional details.\n",
7947                            ioc->name);
7948                        break;
7949                }
7950
7951                break;
7952        }
7953        case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7954        case MPI2_EVENT_IR_OPERATION_STATUS:
7955        case MPI2_EVENT_SAS_DISCOVERY:
7956        case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7957        case MPI2_EVENT_IR_PHYSICAL_DISK:
7958                break;
7959
7960        case MPI2_EVENT_TEMP_THRESHOLD:
7961                _scsih_temp_threshold_events(ioc,
7962                        (Mpi2EventDataTemperature_t *)
7963                        mpi_reply->EventData);
7964                break;
7965
7966        default: /* ignore the rest */
7967                return 1;
7968        }
7969
7970        sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7971        fw_event = alloc_fw_event_work(sz);
7972        if (!fw_event) {
7973                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7974                    ioc->name, __FILE__, __LINE__, __func__);
7975                return 1;
7976        }
7977
7978        memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7979        fw_event->ioc = ioc;
7980        fw_event->VF_ID = mpi_reply->VF_ID;
7981        fw_event->VP_ID = mpi_reply->VP_ID;
7982        fw_event->event = event;
7983        _scsih_fw_event_add(ioc, fw_event);
7984        fw_event_work_put(fw_event);
7985        return 1;
7986}
7987
7988/**
7989 * _scsih_expander_node_remove - removing expander device from list.
7990 * @ioc: per adapter object
7991 * @sas_expander: the sas_device object
7992 * Context: Calling function should acquire ioc->sas_node_lock.
7993 *
7994 * Removing object and freeing associated memory from the
7995 * ioc->sas_expander_list.
7996 *
7997 * Return nothing.
7998 */
7999static void
8000_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8001        struct _sas_node *sas_expander)
8002{
8003        struct _sas_port *mpt3sas_port, *next;
8004
8005        /* remove sibling ports attached to this expander */
8006        list_for_each_entry_safe(mpt3sas_port, next,
8007           &sas_expander->sas_port_list, port_list) {
8008                if (ioc->shost_recovery)
8009                        return;
8010                if (mpt3sas_port->remote_identify.device_type ==
8011                    SAS_END_DEVICE)
8012                        mpt3sas_device_remove_by_sas_address(ioc,
8013                            mpt3sas_port->remote_identify.sas_address);
8014                else if (mpt3sas_port->remote_identify.device_type ==
8015                    SAS_EDGE_EXPANDER_DEVICE ||
8016                    mpt3sas_port->remote_identify.device_type ==
8017                    SAS_FANOUT_EXPANDER_DEVICE)
8018                        mpt3sas_expander_remove(ioc,
8019                            mpt3sas_port->remote_identify.sas_address);
8020        }
8021
8022        mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8023            sas_expander->sas_address_parent);
8024
8025        pr_info(MPT3SAS_FMT
8026                "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8027                ioc->name,
8028            sas_expander->handle, (unsigned long long)
8029            sas_expander->sas_address);
8030
8031        kfree(sas_expander->phy);
8032        kfree(sas_expander);
8033}
8034
8035/**
8036 * _scsih_ir_shutdown - IR shutdown notification
8037 * @ioc: per adapter object
8038 *
8039 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8040 * the host system is shutting down.
8041 *
8042 * Return nothing.
8043 */
8044static void
8045_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8046{
8047        Mpi2RaidActionRequest_t *mpi_request;
8048        Mpi2RaidActionReply_t *mpi_reply;
8049        u16 smid;
8050
8051        /* is IR firmware build loaded ? */
8052        if (!ioc->ir_firmware)
8053                return;
8054
8055        /* are there any volumes ? */
8056        if (list_empty(&ioc->raid_device_list))
8057                return;
8058
8059        mutex_lock(&ioc->scsih_cmds.mutex);
8060
8061        if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8062                pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8063                    ioc->name, __func__);
8064                goto out;
8065        }
8066        ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8067
8068        smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8069        if (!smid) {
8070                pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8071                    ioc->name, __func__);
8072                ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8073                goto out;
8074        }
8075
8076        mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8077        ioc->scsih_cmds.smid = smid;
8078        memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8079
8080        mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8081        mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8082
8083        if (!ioc->hide_ir_msg)
8084                pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
8085        init_completion(&ioc->scsih_cmds.done);
8086        mpt3sas_base_put_smid_default(ioc, smid);
8087        wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8088
8089        if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8090                pr_err(MPT3SAS_FMT "%s: timeout\n",
8091                    ioc->name, __func__);
8092                goto out;
8093        }
8094
8095        if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8096                mpi_reply = ioc->scsih_cmds.reply;
8097                if (!ioc->hide_ir_msg)
8098                        pr_info(MPT3SAS_FMT "IR shutdown "
8099                           "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8100                            ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8101                            le32_to_cpu(mpi_reply->IOCLogInfo));
8102        }
8103
8104 out:
8105        ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8106        mutex_unlock(&ioc->scsih_cmds.mutex);
8107}
8108
8109/**
8110 * scsih_remove - detach and remove add host
8111 * @pdev: PCI device struct
8112 *
8113 * Routine called when unloading the driver.
8114 * Return nothing.
8115 */
8116void scsih_remove(struct pci_dev *pdev)
8117{
8118        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8119        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8120        struct _sas_port *mpt3sas_port, *next_port;
8121        struct _raid_device *raid_device, *next;
8122        struct MPT3SAS_TARGET *sas_target_priv_data;
8123        struct workqueue_struct *wq;
8124        unsigned long flags;
8125
8126        ioc->remove_host = 1;
8127        _scsih_fw_event_cleanup_queue(ioc);
8128
8129        spin_lock_irqsave(&ioc->fw_event_lock, flags);
8130        wq = ioc->firmware_event_thread;
8131        ioc->firmware_event_thread = NULL;
8132        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8133        if (wq)
8134                destroy_workqueue(wq);
8135
8136        /* release all the volumes */
8137        _scsih_ir_shutdown(ioc);
8138        list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8139            list) {
8140                if (raid_device->starget) {
8141                        sas_target_priv_data =
8142                            raid_device->starget->hostdata;
8143                        sas_target_priv_data->deleted = 1;
8144                        scsi_remove_target(&raid_device->starget->dev);
8145                }
8146                pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8147                        ioc->name,  raid_device->handle,
8148                    (unsigned long long) raid_device->wwid);
8149                _scsih_raid_device_remove(ioc, raid_device);
8150        }
8151
8152        /* free ports attached to the sas_host */
8153        list_for_each_entry_safe(mpt3sas_port, next_port,
8154           &ioc->sas_hba.sas_port_list, port_list) {
8155                if (mpt3sas_port->remote_identify.device_type ==
8156                    SAS_END_DEVICE)
8157                        mpt3sas_device_remove_by_sas_address(ioc,
8158                            mpt3sas_port->remote_identify.sas_address);
8159                else if (mpt3sas_port->remote_identify.device_type ==
8160                    SAS_EDGE_EXPANDER_DEVICE ||
8161                    mpt3sas_port->remote_identify.device_type ==
8162                    SAS_FANOUT_EXPANDER_DEVICE)
8163                        mpt3sas_expander_remove(ioc,
8164                            mpt3sas_port->remote_identify.sas_address);
8165        }
8166
8167        /* free phys attached to the sas_host */
8168        if (ioc->sas_hba.num_phys) {
8169                kfree(ioc->sas_hba.phy);
8170                ioc->sas_hba.phy = NULL;
8171                ioc->sas_hba.num_phys = 0;
8172        }
8173
8174        sas_remove_host(shost);
8175        scsi_remove_host(shost);
8176        mpt3sas_base_detach(ioc);
8177        spin_lock(&gioc_lock);
8178        list_del(&ioc->list);
8179        spin_unlock(&gioc_lock);
8180        scsi_host_put(shost);
8181}
8182
8183/**
8184 * scsih_shutdown - routine call during system shutdown
8185 * @pdev: PCI device struct
8186 *
8187 * Return nothing.
8188 */
8189void
8190scsih_shutdown(struct pci_dev *pdev)
8191{
8192        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8193        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8194        struct workqueue_struct *wq;
8195        unsigned long flags;
8196
8197        ioc->remove_host = 1;
8198        _scsih_fw_event_cleanup_queue(ioc);
8199
8200        spin_lock_irqsave(&ioc->fw_event_lock, flags);
8201        wq = ioc->firmware_event_thread;
8202        ioc->firmware_event_thread = NULL;
8203        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8204        if (wq)
8205                destroy_workqueue(wq);
8206
8207        _scsih_ir_shutdown(ioc);
8208        mpt3sas_base_detach(ioc);
8209}
8210
8211
8212/**
8213 * _scsih_probe_boot_devices - reports 1st device
8214 * @ioc: per adapter object
8215 *
8216 * If specified in bios page 2, this routine reports the 1st
8217 * device scsi-ml or sas transport for persistent boot device
8218 * purposes.  Please refer to function _scsih_determine_boot_device()
8219 */
8220static void
8221_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8222{
8223        u8 is_raid;
8224        void *device;
8225        struct _sas_device *sas_device;
8226        struct _raid_device *raid_device;
8227        u16 handle;
8228        u64 sas_address_parent;
8229        u64 sas_address;
8230        unsigned long flags;
8231        int rc;
8232
8233         /* no Bios, return immediately */
8234        if (!ioc->bios_pg3.BiosVersion)
8235                return;
8236
8237        device = NULL;
8238        is_raid = 0;
8239        if (ioc->req_boot_device.device) {
8240                device =  ioc->req_boot_device.device;
8241                is_raid = ioc->req_boot_device.is_raid;
8242        } else if (ioc->req_alt_boot_device.device) {
8243                device =  ioc->req_alt_boot_device.device;
8244                is_raid = ioc->req_alt_boot_device.is_raid;
8245        } else if (ioc->current_boot_device.device) {
8246                device =  ioc->current_boot_device.device;
8247                is_raid = ioc->current_boot_device.is_raid;
8248        }
8249
8250        if (!device)
8251                return;
8252
8253        if (is_raid) {
8254                raid_device = device;
8255                rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8256                    raid_device->id, 0);
8257                if (rc)
8258                        _scsih_raid_device_remove(ioc, raid_device);
8259        } else {
8260                spin_lock_irqsave(&ioc->sas_device_lock, flags);
8261                sas_device = device;
8262                handle = sas_device->handle;
8263                sas_address_parent = sas_device->sas_address_parent;
8264                sas_address = sas_device->sas_address;
8265                list_move_tail(&sas_device->list, &ioc->sas_device_list);
8266                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8267
8268                if (ioc->hide_drives)
8269                        return;
8270                if (!mpt3sas_transport_port_add(ioc, handle,
8271                    sas_address_parent)) {
8272                        _scsih_sas_device_remove(ioc, sas_device);
8273                } else if (!sas_device->starget) {
8274                        if (!ioc->is_driver_loading) {
8275                                mpt3sas_transport_port_remove(ioc,
8276                                    sas_address,
8277                                    sas_address_parent);
8278                                _scsih_sas_device_remove(ioc, sas_device);
8279                        }
8280                }
8281        }
8282}
8283
8284/**
8285 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8286 * @ioc: per adapter object
8287 *
8288 * Called during initial loading of the driver.
8289 */
8290static void
8291_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8292{
8293        struct _raid_device *raid_device, *raid_next;
8294        int rc;
8295
8296        list_for_each_entry_safe(raid_device, raid_next,
8297            &ioc->raid_device_list, list) {
8298                if (raid_device->starget)
8299                        continue;
8300                rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8301                    raid_device->id, 0);
8302                if (rc)
8303                        _scsih_raid_device_remove(ioc, raid_device);
8304        }
8305}
8306
8307static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8308{
8309        struct _sas_device *sas_device = NULL;
8310        unsigned long flags;
8311
8312        spin_lock_irqsave(&ioc->sas_device_lock, flags);
8313        if (!list_empty(&ioc->sas_device_init_list)) {
8314                sas_device = list_first_entry(&ioc->sas_device_init_list,
8315                                struct _sas_device, list);
8316                sas_device_get(sas_device);
8317        }
8318        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8319
8320        return sas_device;
8321}
8322
8323static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8324                struct _sas_device *sas_device)
8325{
8326        unsigned long flags;
8327
8328        spin_lock_irqsave(&ioc->sas_device_lock, flags);
8329
8330        /*
8331         * Since we dropped the lock during the call to port_add(), we need to
8332         * be careful here that somebody else didn't move or delete this item
8333         * while we were busy with other things.
8334         *
8335         * If it was on the list, we need a put() for the reference the list
8336         * had. Either way, we need a get() for the destination list.
8337         */
8338        if (!list_empty(&sas_device->list)) {
8339                list_del_init(&sas_device->list);
8340                sas_device_put(sas_device);
8341        }
8342
8343        sas_device_get(sas_device);
8344        list_add_tail(&sas_device->list, &ioc->sas_device_list);
8345
8346        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8347}
8348
8349/**
8350 * _scsih_probe_sas - reporting sas devices to sas transport
8351 * @ioc: per adapter object
8352 *
8353 * Called during initial loading of the driver.
8354 */
8355static void
8356_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8357{
8358        struct _sas_device *sas_device;
8359
8360        if (ioc->hide_drives)
8361                return;
8362
8363        while ((sas_device = get_next_sas_device(ioc))) {
8364                if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8365                    sas_device->sas_address_parent)) {
8366                        _scsih_sas_device_remove(ioc, sas_device);
8367                        sas_device_put(sas_device);
8368                        continue;
8369                } else if (!sas_device->starget) {
8370                        /*
8371                         * When asyn scanning is enabled, its not possible to
8372                         * remove devices while scanning is turned on due to an
8373                         * oops in scsi_sysfs_add_sdev()->add_device()->
8374                         * sysfs_addrm_start()
8375                         */
8376                        if (!ioc->is_driver_loading) {
8377                                mpt3sas_transport_port_remove(ioc,
8378                                    sas_device->sas_address,
8379                                    sas_device->sas_address_parent);
8380                                _scsih_sas_device_remove(ioc, sas_device);
8381                                sas_device_put(sas_device);
8382                                continue;
8383                        }
8384                }
8385                sas_device_make_active(ioc, sas_device);
8386                sas_device_put(sas_device);
8387        }
8388}
8389
8390/**
8391 * _scsih_probe_devices - probing for devices
8392 * @ioc: per adapter object
8393 *
8394 * Called during initial loading of the driver.
8395 */
8396static void
8397_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8398{
8399        u16 volume_mapping_flags;
8400
8401        if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8402                return;  /* return when IOC doesn't support initiator mode */
8403
8404        _scsih_probe_boot_devices(ioc);
8405
8406        if (ioc->ir_firmware) {
8407                volume_mapping_flags =
8408                    le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8409                    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8410                if (volume_mapping_flags ==
8411                    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8412                        _scsih_probe_raid(ioc);
8413                        _scsih_probe_sas(ioc);
8414                } else {
8415                        _scsih_probe_sas(ioc);
8416                        _scsih_probe_raid(ioc);
8417                }
8418        } else
8419                _scsih_probe_sas(ioc);
8420}
8421
8422/**
8423 * scsih_scan_start - scsi lld callback for .scan_start
8424 * @shost: SCSI host pointer
8425 *
8426 * The shost has the ability to discover targets on its own instead
8427 * of scanning the entire bus.  In our implemention, we will kick off
8428 * firmware discovery.
8429 */
8430void
8431scsih_scan_start(struct Scsi_Host *shost)
8432{
8433        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8434        int rc;
8435        if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8436                mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8437
8438        if (disable_discovery > 0)
8439                return;
8440
8441        ioc->start_scan = 1;
8442        rc = mpt3sas_port_enable(ioc);
8443
8444        if (rc != 0)
8445                pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8446}
8447
8448/**
8449 * scsih_scan_finished - scsi lld callback for .scan_finished
8450 * @shost: SCSI host pointer
8451 * @time: elapsed time of the scan in jiffies
8452 *
8453 * This function will be called periodicallyn until it returns 1 with the
8454 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8455 * we wait for firmware discovery to complete, then return 1.
8456 */
8457int
8458scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8459{
8460        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8461
8462        if (disable_discovery > 0) {
8463                ioc->is_driver_loading = 0;
8464                ioc->wait_for_discovery_to_complete = 0;
8465                return 1;
8466        }
8467
8468        if (time >= (300 * HZ)) {
8469                ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8470                pr_info(MPT3SAS_FMT
8471                        "port enable: FAILED with timeout (timeout=300s)\n",
8472                        ioc->name);
8473                ioc->is_driver_loading = 0;
8474                return 1;
8475        }
8476
8477        if (ioc->start_scan)
8478                return 0;
8479
8480        if (ioc->start_scan_failed) {
8481                pr_info(MPT3SAS_FMT
8482                        "port enable: FAILED with (ioc_status=0x%08x)\n",
8483                        ioc->name, ioc->start_scan_failed);
8484                ioc->is_driver_loading = 0;
8485                ioc->wait_for_discovery_to_complete = 0;
8486                ioc->remove_host = 1;
8487                return 1;
8488        }
8489
8490        pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8491        ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8492
8493        if (ioc->wait_for_discovery_to_complete) {
8494                ioc->wait_for_discovery_to_complete = 0;
8495                _scsih_probe_devices(ioc);
8496        }
8497        mpt3sas_base_start_watchdog(ioc);
8498        ioc->is_driver_loading = 0;
8499        return 1;
8500}
8501
8502/* shost template for SAS 2.0 HBA devices */
8503static struct scsi_host_template mpt2sas_driver_template = {
8504        .module                         = THIS_MODULE,
8505        .name                           = "Fusion MPT SAS Host",
8506        .proc_name                      = MPT2SAS_DRIVER_NAME,
8507        .queuecommand                   = scsih_qcmd,
8508        .target_alloc                   = scsih_target_alloc,
8509        .slave_alloc                    = scsih_slave_alloc,
8510        .slave_configure                = scsih_slave_configure,
8511        .target_destroy                 = scsih_target_destroy,
8512        .slave_destroy                  = scsih_slave_destroy,
8513        .scan_finished                  = scsih_scan_finished,
8514        .scan_start                     = scsih_scan_start,
8515        .change_queue_depth             = scsih_change_queue_depth,
8516        .eh_abort_handler               = scsih_abort,
8517        .eh_device_reset_handler        = scsih_dev_reset,
8518        .eh_target_reset_handler        = scsih_target_reset,
8519        .eh_host_reset_handler          = scsih_host_reset,
8520        .bios_param                     = scsih_bios_param,
8521        .can_queue                      = 1,
8522        .this_id                        = -1,
8523        .sg_tablesize                   = MPT2SAS_SG_DEPTH,
8524        .max_sectors                    = 32767,
8525        .cmd_per_lun                    = 7,
8526        .use_clustering                 = ENABLE_CLUSTERING,
8527        .shost_attrs                    = mpt3sas_host_attrs,
8528        .sdev_attrs                     = mpt3sas_dev_attrs,
8529        .track_queue_depth              = 1,
8530};
8531
8532/* raid transport support for SAS 2.0 HBA devices */
8533static struct raid_function_template mpt2sas_raid_functions = {
8534        .cookie         = &mpt2sas_driver_template,
8535        .is_raid        = scsih_is_raid,
8536        .get_resync     = scsih_get_resync,
8537        .get_state      = scsih_get_state,
8538};
8539
8540/* shost template for SAS 3.0 HBA devices */
8541static struct scsi_host_template mpt3sas_driver_template = {
8542        .module                         = THIS_MODULE,
8543        .name                           = "Fusion MPT SAS Host",
8544        .proc_name                      = MPT3SAS_DRIVER_NAME,
8545        .queuecommand                   = scsih_qcmd,
8546        .target_alloc                   = scsih_target_alloc,
8547        .slave_alloc                    = scsih_slave_alloc,
8548        .slave_configure                = scsih_slave_configure,
8549        .target_destroy                 = scsih_target_destroy,
8550        .slave_destroy                  = scsih_slave_destroy,
8551        .scan_finished                  = scsih_scan_finished,
8552        .scan_start                     = scsih_scan_start,
8553        .change_queue_depth             = scsih_change_queue_depth,
8554        .eh_abort_handler               = scsih_abort,
8555        .eh_device_reset_handler        = scsih_dev_reset,
8556        .eh_target_reset_handler        = scsih_target_reset,
8557        .eh_host_reset_handler          = scsih_host_reset,
8558        .bios_param                     = scsih_bios_param,
8559        .can_queue                      = 1,
8560        .this_id                        = -1,
8561        .sg_tablesize                   = MPT3SAS_SG_DEPTH,
8562        .max_sectors                    = 32767,
8563        .cmd_per_lun                    = 7,
8564        .use_clustering                 = ENABLE_CLUSTERING,
8565        .shost_attrs                    = mpt3sas_host_attrs,
8566        .sdev_attrs                     = mpt3sas_dev_attrs,
8567        .track_queue_depth              = 1,
8568};
8569
8570/* raid transport support for SAS 3.0 HBA devices */
8571static struct raid_function_template mpt3sas_raid_functions = {
8572        .cookie         = &mpt3sas_driver_template,
8573        .is_raid        = scsih_is_raid,
8574        .get_resync     = scsih_get_resync,
8575        .get_state      = scsih_get_state,
8576};
8577
8578/**
8579 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8580 *                                      this device belongs to.
8581 * @pdev: PCI device struct
8582 *
8583 * return MPI2_VERSION for SAS 2.0 HBA devices,
8584 *      MPI25_VERSION for SAS 3.0 HBA devices, and
8585 *      MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
8586 */
8587u16
8588_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8589{
8590
8591        switch (pdev->device) {
8592        case MPI2_MFGPAGE_DEVID_SSS6200:
8593        case MPI2_MFGPAGE_DEVID_SAS2004:
8594        case MPI2_MFGPAGE_DEVID_SAS2008:
8595        case MPI2_MFGPAGE_DEVID_SAS2108_1:
8596        case MPI2_MFGPAGE_DEVID_SAS2108_2:
8597        case MPI2_MFGPAGE_DEVID_SAS2108_3:
8598        case MPI2_MFGPAGE_DEVID_SAS2116_1:
8599        case MPI2_MFGPAGE_DEVID_SAS2116_2:
8600        case MPI2_MFGPAGE_DEVID_SAS2208_1:
8601        case MPI2_MFGPAGE_DEVID_SAS2208_2:
8602        case MPI2_MFGPAGE_DEVID_SAS2208_3:
8603        case MPI2_MFGPAGE_DEVID_SAS2208_4:
8604        case MPI2_MFGPAGE_DEVID_SAS2208_5:
8605        case MPI2_MFGPAGE_DEVID_SAS2208_6:
8606        case MPI2_MFGPAGE_DEVID_SAS2308_1:
8607        case MPI2_MFGPAGE_DEVID_SAS2308_2:
8608        case MPI2_MFGPAGE_DEVID_SAS2308_3:
8609                return MPI2_VERSION;
8610        case MPI25_MFGPAGE_DEVID_SAS3004:
8611        case MPI25_MFGPAGE_DEVID_SAS3008:
8612        case MPI25_MFGPAGE_DEVID_SAS3108_1:
8613        case MPI25_MFGPAGE_DEVID_SAS3108_2:
8614        case MPI25_MFGPAGE_DEVID_SAS3108_5:
8615        case MPI25_MFGPAGE_DEVID_SAS3108_6:
8616                return MPI25_VERSION;
8617        case MPI26_MFGPAGE_DEVID_SAS3216:
8618        case MPI26_MFGPAGE_DEVID_SAS3224:
8619        case MPI26_MFGPAGE_DEVID_SAS3316_1:
8620        case MPI26_MFGPAGE_DEVID_SAS3316_2:
8621        case MPI26_MFGPAGE_DEVID_SAS3316_3:
8622        case MPI26_MFGPAGE_DEVID_SAS3316_4:
8623        case MPI26_MFGPAGE_DEVID_SAS3324_1:
8624        case MPI26_MFGPAGE_DEVID_SAS3324_2:
8625        case MPI26_MFGPAGE_DEVID_SAS3324_3:
8626        case MPI26_MFGPAGE_DEVID_SAS3324_4:
8627                return MPI26_VERSION;
8628        }
8629        return 0;
8630}
8631
8632/**
8633 * _scsih_probe - attach and add scsi host
8634 * @pdev: PCI device struct
8635 * @id: pci device id
8636 *
8637 * Returns 0 success, anything else error.
8638 */
8639int
8640_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8641{
8642        struct MPT3SAS_ADAPTER *ioc;
8643        struct Scsi_Host *shost = NULL;
8644        int rv;
8645        u16 hba_mpi_version;
8646
8647        /* Determine in which MPI version class this pci device belongs */
8648        hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8649        if (hba_mpi_version == 0)
8650                return -ENODEV;
8651
8652        /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8653         * for other generation HBA's return with -ENODEV
8654         */
8655        if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
8656                return -ENODEV;
8657
8658        /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8659         * for other generation HBA's return with -ENODEV
8660         */
8661        if ((hbas_to_enumerate == 2) && (!(hba_mpi_version ==  MPI25_VERSION
8662                || hba_mpi_version ==  MPI26_VERSION)))
8663                return -ENODEV;
8664
8665        switch (hba_mpi_version) {
8666        case MPI2_VERSION:
8667                /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8668                shost = scsi_host_alloc(&mpt2sas_driver_template,
8669                  sizeof(struct MPT3SAS_ADAPTER));
8670                if (!shost)
8671                        return -ENODEV;
8672                ioc = shost_priv(shost);
8673                memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8674                ioc->hba_mpi_version_belonged = hba_mpi_version;
8675                ioc->id = mpt2_ids++;
8676                sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8677                if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8678                        ioc->is_warpdrive = 1;
8679                        ioc->hide_ir_msg = 1;
8680                } else
8681                        ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8682                break;
8683        case MPI25_VERSION:
8684        case MPI26_VERSION:
8685                /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8686                shost = scsi_host_alloc(&mpt3sas_driver_template,
8687                  sizeof(struct MPT3SAS_ADAPTER));
8688                if (!shost)
8689                        return -ENODEV;
8690                ioc = shost_priv(shost);
8691                memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8692                ioc->hba_mpi_version_belonged = hba_mpi_version;
8693                ioc->id = mpt3_ids++;
8694                sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8695                if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8696                        pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
8697                        (ioc->hba_mpi_version_belonged == MPI26_VERSION))
8698                        ioc->msix96_vector = 1;
8699                break;
8700        default:
8701                return -ENODEV;
8702        }
8703
8704        INIT_LIST_HEAD(&ioc->list);
8705        spin_lock(&gioc_lock);
8706        list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8707        spin_unlock(&gioc_lock);
8708        ioc->shost = shost;
8709        ioc->pdev = pdev;
8710        ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8711        ioc->tm_cb_idx = tm_cb_idx;
8712        ioc->ctl_cb_idx = ctl_cb_idx;
8713        ioc->base_cb_idx = base_cb_idx;
8714        ioc->port_enable_cb_idx = port_enable_cb_idx;
8715        ioc->transport_cb_idx = transport_cb_idx;
8716        ioc->scsih_cb_idx = scsih_cb_idx;
8717        ioc->config_cb_idx = config_cb_idx;
8718        ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8719        ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8720        ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8721        ioc->logging_level = logging_level;
8722        ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8723        /* misc semaphores and spin locks */
8724        mutex_init(&ioc->reset_in_progress_mutex);
8725        /* initializing pci_access_mutex lock */
8726        mutex_init(&ioc->pci_access_mutex);
8727        spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8728        spin_lock_init(&ioc->scsi_lookup_lock);
8729        spin_lock_init(&ioc->sas_device_lock);
8730        spin_lock_init(&ioc->sas_node_lock);
8731        spin_lock_init(&ioc->fw_event_lock);
8732        spin_lock_init(&ioc->raid_device_lock);
8733        spin_lock_init(&ioc->diag_trigger_lock);
8734
8735        INIT_LIST_HEAD(&ioc->sas_device_list);
8736        INIT_LIST_HEAD(&ioc->sas_device_init_list);
8737        INIT_LIST_HEAD(&ioc->sas_expander_list);
8738        INIT_LIST_HEAD(&ioc->fw_event_list);
8739        INIT_LIST_HEAD(&ioc->raid_device_list);
8740        INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8741        INIT_LIST_HEAD(&ioc->delayed_tr_list);
8742        INIT_LIST_HEAD(&ioc->delayed_sc_list);
8743        INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
8744        INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8745        INIT_LIST_HEAD(&ioc->reply_queue_list);
8746
8747        sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8748
8749        /* init shost parameters */
8750        shost->max_cmd_len = 32;
8751        shost->max_lun = max_lun;
8752        shost->transportt = mpt3sas_transport_template;
8753        shost->unique_id = ioc->id;
8754
8755        if (max_sectors != 0xFFFF) {
8756                if (max_sectors < 64) {
8757                        shost->max_sectors = 64;
8758                        pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8759                            "for max_sectors, range is 64 to 32767. Assigning "
8760                            "value of 64.\n", ioc->name, max_sectors);
8761                } else if (max_sectors > 32767) {
8762                        shost->max_sectors = 32767;
8763                        pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8764                            "for max_sectors, range is 64 to 32767. Assigning "
8765                            "default value of 32767.\n", ioc->name,
8766                            max_sectors);
8767                } else {
8768                        shost->max_sectors = max_sectors & 0xFFFE;
8769                        pr_info(MPT3SAS_FMT
8770                                "The max_sectors value is set to %d\n",
8771                                ioc->name, shost->max_sectors);
8772                }
8773        }
8774
8775        /* register EEDP capabilities with SCSI layer */
8776        if (prot_mask > 0)
8777                scsi_host_set_prot(shost, prot_mask);
8778        else
8779                scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8780                                   | SHOST_DIF_TYPE2_PROTECTION
8781                                   | SHOST_DIF_TYPE3_PROTECTION);
8782
8783        scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8784
8785        /* event thread */
8786        snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8787            "fw_event_%s%d", ioc->driver_name, ioc->id);
8788        ioc->firmware_event_thread = alloc_ordered_workqueue(
8789            ioc->firmware_event_name, WQ_MEM_RECLAIM);
8790        if (!ioc->firmware_event_thread) {
8791                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8792                    ioc->name, __FILE__, __LINE__, __func__);
8793                rv = -ENODEV;
8794                goto out_thread_fail;
8795        }
8796
8797        ioc->is_driver_loading = 1;
8798        if ((mpt3sas_base_attach(ioc))) {
8799                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8800                    ioc->name, __FILE__, __LINE__, __func__);
8801                rv = -ENODEV;
8802                goto out_attach_fail;
8803        }
8804
8805        if (ioc->is_warpdrive) {
8806                if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
8807                        ioc->hide_drives = 0;
8808                else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
8809                        ioc->hide_drives = 1;
8810                else {
8811                        if (mpt3sas_get_num_volumes(ioc))
8812                                ioc->hide_drives = 1;
8813                        else
8814                                ioc->hide_drives = 0;
8815                }
8816        } else
8817                ioc->hide_drives = 0;
8818
8819        rv = scsi_add_host(shost, &pdev->dev);
8820        if (rv) {
8821                pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8822                    ioc->name, __FILE__, __LINE__, __func__);
8823                goto out_add_shost_fail;
8824        }
8825
8826        scsi_scan_host(shost);
8827        return 0;
8828out_add_shost_fail:
8829        mpt3sas_base_detach(ioc);
8830 out_attach_fail:
8831        destroy_workqueue(ioc->firmware_event_thread);
8832 out_thread_fail:
8833        spin_lock(&gioc_lock);
8834        list_del(&ioc->list);
8835        spin_unlock(&gioc_lock);
8836        scsi_host_put(shost);
8837        return rv;
8838}
8839
8840#ifdef CONFIG_PM
8841/**
8842 * scsih_suspend - power management suspend main entry point
8843 * @pdev: PCI device struct
8844 * @state: PM state change to (usually PCI_D3)
8845 *
8846 * Returns 0 success, anything else error.
8847 */
8848int
8849scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8850{
8851        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8852        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8853        pci_power_t device_state;
8854
8855        mpt3sas_base_stop_watchdog(ioc);
8856        flush_scheduled_work();
8857        scsi_block_requests(shost);
8858        device_state = pci_choose_state(pdev, state);
8859        pr_info(MPT3SAS_FMT
8860                "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8861                ioc->name, pdev, pci_name(pdev), device_state);
8862
8863        pci_save_state(pdev);
8864        mpt3sas_base_free_resources(ioc);
8865        pci_set_power_state(pdev, device_state);
8866        return 0;
8867}
8868
8869/**
8870 * scsih_resume - power management resume main entry point
8871 * @pdev: PCI device struct
8872 *
8873 * Returns 0 success, anything else error.
8874 */
8875int
8876scsih_resume(struct pci_dev *pdev)
8877{
8878        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8879        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8880        pci_power_t device_state = pdev->current_state;
8881        int r;
8882
8883        pr_info(MPT3SAS_FMT
8884                "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8885                ioc->name, pdev, pci_name(pdev), device_state);
8886
8887        pci_set_power_state(pdev, PCI_D0);
8888        pci_enable_wake(pdev, PCI_D0, 0);
8889        pci_restore_state(pdev);
8890        ioc->pdev = pdev;
8891        r = mpt3sas_base_map_resources(ioc);
8892        if (r)
8893                return r;
8894
8895        mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8896        scsi_unblock_requests(shost);
8897        mpt3sas_base_start_watchdog(ioc);
8898        return 0;
8899}
8900#endif /* CONFIG_PM */
8901
8902/**
8903 * scsih_pci_error_detected - Called when a PCI error is detected.
8904 * @pdev: PCI device struct
8905 * @state: PCI channel state
8906 *
8907 * Description: Called when a PCI error is detected.
8908 *
8909 * Return value:
8910 *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8911 */
8912pci_ers_result_t
8913scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8914{
8915        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8916        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8917
8918        pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8919            ioc->name, state);
8920
8921        switch (state) {
8922        case pci_channel_io_normal:
8923                return PCI_ERS_RESULT_CAN_RECOVER;
8924        case pci_channel_io_frozen:
8925                /* Fatal error, prepare for slot reset */
8926                ioc->pci_error_recovery = 1;
8927                scsi_block_requests(ioc->shost);
8928                mpt3sas_base_stop_watchdog(ioc);
8929                mpt3sas_base_free_resources(ioc);
8930                return PCI_ERS_RESULT_NEED_RESET;
8931        case pci_channel_io_perm_failure:
8932                /* Permanent error, prepare for device removal */
8933                ioc->pci_error_recovery = 1;
8934                mpt3sas_base_stop_watchdog(ioc);
8935                _scsih_flush_running_cmds(ioc);
8936                return PCI_ERS_RESULT_DISCONNECT;
8937        }
8938        return PCI_ERS_RESULT_NEED_RESET;
8939}
8940
8941/**
8942 * scsih_pci_slot_reset - Called when PCI slot has been reset.
8943 * @pdev: PCI device struct
8944 *
8945 * Description: This routine is called by the pci error recovery
8946 * code after the PCI slot has been reset, just before we
8947 * should resume normal operations.
8948 */
8949pci_ers_result_t
8950scsih_pci_slot_reset(struct pci_dev *pdev)
8951{
8952        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8953        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8954        int rc;
8955
8956        pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8957             ioc->name);
8958
8959        ioc->pci_error_recovery = 0;
8960        ioc->pdev = pdev;
8961        pci_restore_state(pdev);
8962        rc = mpt3sas_base_map_resources(ioc);
8963        if (rc)
8964                return PCI_ERS_RESULT_DISCONNECT;
8965
8966        rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8967            FORCE_BIG_HAMMER);
8968
8969        pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8970            (rc == 0) ? "success" : "failed");
8971
8972        if (!rc)
8973                return PCI_ERS_RESULT_RECOVERED;
8974        else
8975                return PCI_ERS_RESULT_DISCONNECT;
8976}
8977
8978/**
8979 * scsih_pci_resume() - resume normal ops after PCI reset
8980 * @pdev: pointer to PCI device
8981 *
8982 * Called when the error recovery driver tells us that its
8983 * OK to resume normal operation. Use completion to allow
8984 * halted scsi ops to resume.
8985 */
8986void
8987scsih_pci_resume(struct pci_dev *pdev)
8988{
8989        struct Scsi_Host *shost = pci_get_drvdata(pdev);
8990        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8991
8992        pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8993
8994        pci_cleanup_aer_uncorrect_error_status(pdev);
8995        mpt3sas_base_start_watchdog(ioc);
8996        scsi_unblock_requests(ioc->shost);
8997}
8998
8999/**
9000 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
9001 * @pdev: pointer to PCI device
9002 */
9003pci_ers_result_t
9004scsih_pci_mmio_enabled(struct pci_dev *pdev)
9005{
9006        struct Scsi_Host *shost = pci_get_drvdata(pdev);
9007        struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9008
9009        pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9010            ioc->name);
9011
9012        /* TODO - dump whatever for debugging purposes */
9013
9014        /* Request a slot reset. */
9015        return PCI_ERS_RESULT_NEED_RESET;
9016}
9017
9018/*
9019 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9020 */
9021static const struct pci_device_id mpt3sas_pci_table[] = {
9022        /* Spitfire ~ 2004 */
9023        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9024                PCI_ANY_ID, PCI_ANY_ID },
9025        /* Falcon ~ 2008 */
9026        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9027                PCI_ANY_ID, PCI_ANY_ID },
9028        /* Liberator ~ 2108 */
9029        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9030                PCI_ANY_ID, PCI_ANY_ID },
9031        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9032                PCI_ANY_ID, PCI_ANY_ID },
9033        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9034                PCI_ANY_ID, PCI_ANY_ID },
9035        /* Meteor ~ 2116 */
9036        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9037                PCI_ANY_ID, PCI_ANY_ID },
9038        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9039                PCI_ANY_ID, PCI_ANY_ID },
9040        /* Thunderbolt ~ 2208 */
9041        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9042                PCI_ANY_ID, PCI_ANY_ID },
9043        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9044                PCI_ANY_ID, PCI_ANY_ID },
9045        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9046                PCI_ANY_ID, PCI_ANY_ID },
9047        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9048                PCI_ANY_ID, PCI_ANY_ID },
9049        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9050                PCI_ANY_ID, PCI_ANY_ID },
9051        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9052                PCI_ANY_ID, PCI_ANY_ID },
9053        /* Mustang ~ 2308 */
9054        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9055                PCI_ANY_ID, PCI_ANY_ID },
9056        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9057                PCI_ANY_ID, PCI_ANY_ID },
9058        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9059                PCI_ANY_ID, PCI_ANY_ID },
9060        /* SSS6200 */
9061        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9062                PCI_ANY_ID, PCI_ANY_ID },
9063        /* Fury ~ 3004 and 3008 */
9064        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9065                PCI_ANY_ID, PCI_ANY_ID },
9066        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9067                PCI_ANY_ID, PCI_ANY_ID },
9068        /* Invader ~ 3108 */
9069        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9070                PCI_ANY_ID, PCI_ANY_ID },
9071        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9072                PCI_ANY_ID, PCI_ANY_ID },
9073        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9074                PCI_ANY_ID, PCI_ANY_ID },
9075        { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9076                PCI_ANY_ID, PCI_ANY_ID },
9077        /* Cutlass ~ 3216 and 3224 */
9078        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9079                PCI_ANY_ID, PCI_ANY_ID },
9080        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9081                PCI_ANY_ID, PCI_ANY_ID },
9082        /* Intruder ~ 3316 and 3324 */
9083        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9084                PCI_ANY_ID, PCI_ANY_ID },
9085        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9086                PCI_ANY_ID, PCI_ANY_ID },
9087        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9088                PCI_ANY_ID, PCI_ANY_ID },
9089        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9090                PCI_ANY_ID, PCI_ANY_ID },
9091        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9092                PCI_ANY_ID, PCI_ANY_ID },
9093        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9094                PCI_ANY_ID, PCI_ANY_ID },
9095        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9096                PCI_ANY_ID, PCI_ANY_ID },
9097        { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9098                PCI_ANY_ID, PCI_ANY_ID },
9099        {0}     /* Terminating entry */
9100};
9101MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9102
9103static struct pci_error_handlers _mpt3sas_err_handler = {
9104        .error_detected = scsih_pci_error_detected,
9105        .mmio_enabled   = scsih_pci_mmio_enabled,
9106        .slot_reset     = scsih_pci_slot_reset,
9107        .resume         = scsih_pci_resume,
9108};
9109
9110static struct pci_driver mpt3sas_driver = {
9111        .name           = MPT3SAS_DRIVER_NAME,
9112        .id_table       = mpt3sas_pci_table,
9113        .probe          = _scsih_probe,
9114        .remove         = scsih_remove,
9115        .shutdown       = scsih_shutdown,
9116        .err_handler    = &_mpt3sas_err_handler,
9117#ifdef CONFIG_PM
9118        .suspend        = scsih_suspend,
9119        .resume         = scsih_resume,
9120#endif
9121};
9122
9123/**
9124 * scsih_init - main entry point for this driver.
9125 *
9126 * Returns 0 success, anything else error.
9127 */
9128int
9129scsih_init(void)
9130{
9131        mpt2_ids = 0;
9132        mpt3_ids = 0;
9133
9134        mpt3sas_base_initialize_callback_handler();
9135
9136         /* queuecommand callback hander */
9137        scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9138
9139        /* task managment callback handler */
9140        tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9141
9142        /* base internal commands callback handler */
9143        base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9144        port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9145            mpt3sas_port_enable_done);
9146
9147        /* transport internal commands callback handler */
9148        transport_cb_idx = mpt3sas_base_register_callback_handler(
9149            mpt3sas_transport_done);
9150
9151        /* scsih internal commands callback handler */
9152        scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9153
9154        /* configuration page API internal commands callback handler */
9155        config_cb_idx = mpt3sas_base_register_callback_handler(
9156            mpt3sas_config_done);
9157
9158        /* ctl module callback handler */
9159        ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9160
9161        tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9162            _scsih_tm_tr_complete);
9163
9164        tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9165            _scsih_tm_volume_tr_complete);
9166
9167        tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9168            _scsih_sas_control_complete);
9169
9170        return 0;
9171}
9172
9173/**
9174 * scsih_exit - exit point for this driver (when it is a module).
9175 *
9176 * Returns 0 success, anything else error.
9177 */
9178void
9179scsih_exit(void)
9180{
9181
9182        mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9183        mpt3sas_base_release_callback_handler(tm_cb_idx);
9184        mpt3sas_base_release_callback_handler(base_cb_idx);
9185        mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9186        mpt3sas_base_release_callback_handler(transport_cb_idx);
9187        mpt3sas_base_release_callback_handler(scsih_cb_idx);
9188        mpt3sas_base_release_callback_handler(config_cb_idx);
9189        mpt3sas_base_release_callback_handler(ctl_cb_idx);
9190
9191        mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9192        mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9193        mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9194
9195/* raid transport support */
9196        if (hbas_to_enumerate != 1)
9197                raid_class_release(mpt3sas_raid_template);
9198        if (hbas_to_enumerate != 2)
9199                raid_class_release(mpt2sas_raid_template);
9200        sas_release_transport(mpt3sas_transport_template);
9201}
9202
9203/**
9204 * _mpt3sas_init - main entry point for this driver.
9205 *
9206 * Returns 0 success, anything else error.
9207 */
9208static int __init
9209_mpt3sas_init(void)
9210{
9211        int error;
9212
9213        pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9214                                        MPT3SAS_DRIVER_VERSION);
9215
9216        mpt3sas_transport_template =
9217            sas_attach_transport(&mpt3sas_transport_functions);
9218        if (!mpt3sas_transport_template)
9219                return -ENODEV;
9220
9221        /* No need attach mpt3sas raid functions template
9222         * if hbas_to_enumarate value is one.
9223         */
9224        if (hbas_to_enumerate != 1) {
9225                mpt3sas_raid_template =
9226                                raid_class_attach(&mpt3sas_raid_functions);
9227                if (!mpt3sas_raid_template) {
9228                        sas_release_transport(mpt3sas_transport_template);
9229                        return -ENODEV;
9230                }
9231        }
9232
9233        /* No need to attach mpt2sas raid functions template
9234         * if hbas_to_enumarate value is two
9235         */
9236        if (hbas_to_enumerate != 2) {
9237                mpt2sas_raid_template =
9238                                raid_class_attach(&mpt2sas_raid_functions);
9239                if (!mpt2sas_raid_template) {
9240                        sas_release_transport(mpt3sas_transport_template);
9241                        return -ENODEV;
9242                }
9243        }
9244
9245        error = scsih_init();
9246        if (error) {
9247                scsih_exit();
9248                return error;
9249        }
9250
9251        mpt3sas_ctl_init(hbas_to_enumerate);
9252
9253        error = pci_register_driver(&mpt3sas_driver);
9254        if (error)
9255                scsih_exit();
9256
9257        return error;
9258}
9259
9260/**
9261 * _mpt3sas_exit - exit point for this driver (when it is a module).
9262 *
9263 */
9264static void __exit
9265_mpt3sas_exit(void)
9266{
9267        pr_info("mpt3sas version %s unloading\n",
9268                                MPT3SAS_DRIVER_VERSION);
9269
9270        pci_unregister_driver(&mpt3sas_driver);
9271
9272        mpt3sas_ctl_exit(hbas_to_enumerate);
9273
9274        scsih_exit();
9275}
9276
9277module_init(_mpt3sas_init);
9278module_exit(_mpt3sas_exit);
9279