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