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