linux/drivers/scsi/mpt2sas/mpt2sas_scsih.c
<<
>>
Prefs
   1/*
   2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
   3 *
   4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
   5 * Copyright (C) 2007-2010  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#include <linux/slab.h>
  57
  58#include "mpt2sas_base.h"
  59
  60MODULE_AUTHOR(MPT2SAS_AUTHOR);
  61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
  62MODULE_LICENSE("GPL");
  63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
  64
  65#define RAID_CHANNEL 1
  66
  67/* forward proto's */
  68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  69    struct _sas_node *sas_expander);
  70static void _firmware_event_work(struct work_struct *work);
  71
  72static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
  73
  74static void _scsih_scan_start(struct Scsi_Host *shost);
  75static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
  76
  77/* global parameters */
  78LIST_HEAD(mpt2sas_ioc_list);
  79
  80/* local parameters */
  81static u8 scsi_io_cb_idx = -1;
  82static u8 tm_cb_idx = -1;
  83static u8 ctl_cb_idx = -1;
  84static u8 base_cb_idx = -1;
  85static u8 port_enable_cb_idx = -1;
  86static u8 transport_cb_idx = -1;
  87static u8 scsih_cb_idx = -1;
  88static u8 config_cb_idx = -1;
  89static int mpt_ids;
  90
  91static u8 tm_tr_cb_idx = -1 ;
  92static u8 tm_tr_volume_cb_idx = -1 ;
  93static u8 tm_sas_control_cb_idx = -1;
  94
  95/* command line options */
  96static u32 logging_level;
  97MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
  98    "(default=0)");
  99
 100static ushort max_sectors = 0xFFFF;
 101module_param(max_sectors, ushort, 0);
 102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 8192  default=8192");
 103
 104/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
 105#define MPT2SAS_MAX_LUN (16895)
 106static int max_lun = MPT2SAS_MAX_LUN;
 107module_param(max_lun, int, 0);
 108MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
 109
 110/* diag_buffer_enable is bitwise
 111 * bit 0 set = TRACE
 112 * bit 1 set = SNAPSHOT
 113 * bit 2 set = EXTENDED
 114 *
 115 * Either bit can be set, or both
 116 */
 117static int diag_buffer_enable = -1;
 118module_param(diag_buffer_enable, int, 0);
 119MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
 120        "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
 121
 122/**
 123 * struct sense_info - common structure for obtaining sense keys
 124 * @skey: sense key
 125 * @asc: additional sense code
 126 * @ascq: additional sense code qualifier
 127 */
 128struct sense_info {
 129        u8 skey;
 130        u8 asc;
 131        u8 ascq;
 132};
 133
 134
 135#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
 136#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
 137#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
 138/**
 139 * struct fw_event_work - firmware event struct
 140 * @list: link list framework
 141 * @work: work object (ioc->fault_reset_work_q)
 142 * @cancel_pending_work: flag set during reset handling
 143 * @ioc: per adapter object
 144 * @device_handle: device handle
 145 * @VF_ID: virtual function id
 146 * @VP_ID: virtual port id
 147 * @ignore: flag meaning this event has been marked to ignore
 148 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
 149 * @event_data: reply event data payload follows
 150 *
 151 * This object stored on ioc->fw_event_list.
 152 */
 153struct fw_event_work {
 154        struct list_head        list;
 155        u8                      cancel_pending_work;
 156        struct delayed_work     delayed_work;
 157        struct MPT2SAS_ADAPTER *ioc;
 158        u16                     device_handle;
 159        u8                      VF_ID;
 160        u8                      VP_ID;
 161        u8                      ignore;
 162        u16                     event;
 163        void                    *event_data;
 164};
 165
 166/* raid transport support */
 167static struct raid_template *mpt2sas_raid_template;
 168
 169/**
 170 * struct _scsi_io_transfer - scsi io transfer
 171 * @handle: sas device handle (assigned by firmware)
 172 * @is_raid: flag set for hidden raid components
 173 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
 174 * @data_length: data transfer length
 175 * @data_dma: dma pointer to data
 176 * @sense: sense data
 177 * @lun: lun number
 178 * @cdb_length: cdb length
 179 * @cdb: cdb contents
 180 * @timeout: timeout for this command
 181 * @VF_ID: virtual function id
 182 * @VP_ID: virtual port id
 183 * @valid_reply: flag set for reply message
 184 * @sense_length: sense length
 185 * @ioc_status: ioc status
 186 * @scsi_state: scsi state
 187 * @scsi_status: scsi staus
 188 * @log_info: log information
 189 * @transfer_length: data length transfer when there is a reply message
 190 *
 191 * Used for sending internal scsi commands to devices within this module.
 192 * Refer to _scsi_send_scsi_io().
 193 */
 194struct _scsi_io_transfer {
 195        u16     handle;
 196        u8      is_raid;
 197        enum dma_data_direction dir;
 198        u32     data_length;
 199        dma_addr_t data_dma;
 200        u8      sense[SCSI_SENSE_BUFFERSIZE];
 201        u32     lun;
 202        u8      cdb_length;
 203        u8      cdb[32];
 204        u8      timeout;
 205        u8      VF_ID;
 206        u8      VP_ID;
 207        u8      valid_reply;
 208  /* the following bits are only valid when 'valid_reply = 1' */
 209        u32     sense_length;
 210        u16     ioc_status;
 211        u8      scsi_state;
 212        u8      scsi_status;
 213        u32     log_info;
 214        u32     transfer_length;
 215};
 216
 217/*
 218 * The pci device ids are defined in mpi/mpi2_cnfg.h.
 219 */
 220static struct pci_device_id scsih_pci_table[] = {
 221        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
 222                PCI_ANY_ID, PCI_ANY_ID },
 223        /* Falcon ~ 2008*/
 224        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
 225                PCI_ANY_ID, PCI_ANY_ID },
 226        /* Liberator ~ 2108 */
 227        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
 228                PCI_ANY_ID, PCI_ANY_ID },
 229        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
 230                PCI_ANY_ID, PCI_ANY_ID },
 231        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
 232                PCI_ANY_ID, PCI_ANY_ID },
 233        /* Meteor ~ 2116 */
 234        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
 235                PCI_ANY_ID, PCI_ANY_ID },
 236        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
 237                PCI_ANY_ID, PCI_ANY_ID },
 238        /* Thunderbolt ~ 2208 */
 239        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
 240                PCI_ANY_ID, PCI_ANY_ID },
 241        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
 242                PCI_ANY_ID, PCI_ANY_ID },
 243        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
 244                PCI_ANY_ID, PCI_ANY_ID },
 245        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
 246                PCI_ANY_ID, PCI_ANY_ID },
 247        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
 248                PCI_ANY_ID, PCI_ANY_ID },
 249        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
 250                PCI_ANY_ID, PCI_ANY_ID },
 251        /* Mustang ~ 2308 */
 252        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
 253                PCI_ANY_ID, PCI_ANY_ID },
 254        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
 255                PCI_ANY_ID, PCI_ANY_ID },
 256        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
 257                PCI_ANY_ID, PCI_ANY_ID },
 258        /* SSS6200 */
 259        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
 260                PCI_ANY_ID, PCI_ANY_ID },
 261        {0}     /* Terminating entry */
 262};
 263MODULE_DEVICE_TABLE(pci, scsih_pci_table);
 264
 265/**
 266 * _scsih_set_debug_level - global setting of ioc->logging_level.
 267 *
 268 * Note: The logging levels are defined in mpt2sas_debug.h.
 269 */
 270static int
 271_scsih_set_debug_level(const char *val, struct kernel_param *kp)
 272{
 273        int ret = param_set_int(val, kp);
 274        struct MPT2SAS_ADAPTER *ioc;
 275
 276        if (ret)
 277                return ret;
 278
 279        printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
 280        list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
 281                ioc->logging_level = logging_level;
 282        return 0;
 283}
 284module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
 285    &logging_level, 0644);
 286
 287/**
 288 * _scsih_srch_boot_sas_address - search based on sas_address
 289 * @sas_address: sas address
 290 * @boot_device: boot device object from bios page 2
 291 *
 292 * Returns 1 when there's a match, 0 means no match.
 293 */
 294static inline int
 295_scsih_srch_boot_sas_address(u64 sas_address,
 296    Mpi2BootDeviceSasWwid_t *boot_device)
 297{
 298        return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
 299}
 300
 301/**
 302 * _scsih_srch_boot_device_name - search based on device name
 303 * @device_name: device name specified in INDENTIFY fram
 304 * @boot_device: boot device object from bios page 2
 305 *
 306 * Returns 1 when there's a match, 0 means no match.
 307 */
 308static inline int
 309_scsih_srch_boot_device_name(u64 device_name,
 310    Mpi2BootDeviceDeviceName_t *boot_device)
 311{
 312        return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
 313}
 314
 315/**
 316 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
 317 * @enclosure_logical_id: enclosure logical id
 318 * @slot_number: slot number
 319 * @boot_device: boot device object from bios page 2
 320 *
 321 * Returns 1 when there's a match, 0 means no match.
 322 */
 323static inline int
 324_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
 325    Mpi2BootDeviceEnclosureSlot_t *boot_device)
 326{
 327        return (enclosure_logical_id == le64_to_cpu(boot_device->
 328            EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
 329            SlotNumber)) ? 1 : 0;
 330}
 331
 332/**
 333 * _scsih_is_boot_device - search for matching boot device.
 334 * @sas_address: sas address
 335 * @device_name: device name specified in INDENTIFY fram
 336 * @enclosure_logical_id: enclosure logical id
 337 * @slot_number: slot number
 338 * @form: specifies boot device form
 339 * @boot_device: boot device object from bios page 2
 340 *
 341 * Returns 1 when there's a match, 0 means no match.
 342 */
 343static int
 344_scsih_is_boot_device(u64 sas_address, u64 device_name,
 345    u64 enclosure_logical_id, u16 slot, u8 form,
 346    Mpi2BiosPage2BootDevice_t *boot_device)
 347{
 348        int rc = 0;
 349
 350        switch (form) {
 351        case MPI2_BIOSPAGE2_FORM_SAS_WWID:
 352                if (!sas_address)
 353                        break;
 354                rc = _scsih_srch_boot_sas_address(
 355                    sas_address, &boot_device->SasWwid);
 356                break;
 357        case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
 358                if (!enclosure_logical_id)
 359                        break;
 360                rc = _scsih_srch_boot_encl_slot(
 361                    enclosure_logical_id,
 362                    slot, &boot_device->EnclosureSlot);
 363                break;
 364        case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
 365                if (!device_name)
 366                        break;
 367                rc = _scsih_srch_boot_device_name(
 368                    device_name, &boot_device->DeviceName);
 369                break;
 370        case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
 371                break;
 372        }
 373
 374        return rc;
 375}
 376
 377/**
 378 * _scsih_get_sas_address - set the sas_address for given device handle
 379 * @handle: device handle
 380 * @sas_address: sas address
 381 *
 382 * Returns 0 success, non-zero when failure
 383 */
 384static int
 385_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
 386    u64 *sas_address)
 387{
 388        Mpi2SasDevicePage0_t sas_device_pg0;
 389        Mpi2ConfigReply_t mpi_reply;
 390        u32 ioc_status;
 391        *sas_address = 0;
 392
 393        if (handle <= ioc->sas_hba.num_phys) {
 394                *sas_address = ioc->sas_hba.sas_address;
 395                return 0;
 396        }
 397
 398        if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
 399            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
 400                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
 401                __FILE__, __LINE__, __func__);
 402                return -ENXIO;
 403        }
 404
 405        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
 406        if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
 407                *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
 408                return 0;
 409        }
 410
 411        /* we hit this becuase the given parent handle doesn't exist */
 412        if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
 413                return -ENXIO;
 414        /* else error case */
 415        printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
 416            "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
 417             __FILE__, __LINE__, __func__);
 418        return -EIO;
 419}
 420
 421/**
 422 * _scsih_determine_boot_device - determine boot device.
 423 * @ioc: per adapter object
 424 * @device: either sas_device or raid_device object
 425 * @is_raid: [flag] 1 = raid object, 0 = sas object
 426 *
 427 * Determines whether this device should be first reported device to
 428 * to scsi-ml or sas transport, this purpose is for persistent boot device.
 429 * There are primary, alternate, and current entries in bios page 2. The order
 430 * priority is primary, alternate, then current.  This routine saves
 431 * the corresponding device object and is_raid flag in the ioc object.
 432 * The saved data to be used later in _scsih_probe_boot_devices().
 433 */
 434static void
 435_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
 436    void *device, u8 is_raid)
 437{
 438        struct _sas_device *sas_device;
 439        struct _raid_device *raid_device;
 440        u64 sas_address;
 441        u64 device_name;
 442        u64 enclosure_logical_id;
 443        u16 slot;
 444
 445         /* only process this function when driver loads */
 446        if (!ioc->is_driver_loading)
 447                return;
 448
 449         /* no Bios, return immediately */
 450        if (!ioc->bios_pg3.BiosVersion)
 451                return;
 452
 453        if (!is_raid) {
 454                sas_device = device;
 455                sas_address = sas_device->sas_address;
 456                device_name = sas_device->device_name;
 457                enclosure_logical_id = sas_device->enclosure_logical_id;
 458                slot = sas_device->slot;
 459        } else {
 460                raid_device = device;
 461                sas_address = raid_device->wwid;
 462                device_name = 0;
 463                enclosure_logical_id = 0;
 464                slot = 0;
 465        }
 466
 467        if (!ioc->req_boot_device.device) {
 468                if (_scsih_is_boot_device(sas_address, device_name,
 469                    enclosure_logical_id, slot,
 470                    (ioc->bios_pg2.ReqBootDeviceForm &
 471                    MPI2_BIOSPAGE2_FORM_MASK),
 472                    &ioc->bios_pg2.RequestedBootDevice)) {
 473                        dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
 474                           "%s: req_boot_device(0x%016llx)\n",
 475                            ioc->name, __func__,
 476                            (unsigned long long)sas_address));
 477                        ioc->req_boot_device.device = device;
 478                        ioc->req_boot_device.is_raid = is_raid;
 479                }
 480        }
 481
 482        if (!ioc->req_alt_boot_device.device) {
 483                if (_scsih_is_boot_device(sas_address, device_name,
 484                    enclosure_logical_id, slot,
 485                    (ioc->bios_pg2.ReqAltBootDeviceForm &
 486                    MPI2_BIOSPAGE2_FORM_MASK),
 487                    &ioc->bios_pg2.RequestedAltBootDevice)) {
 488                        dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
 489                           "%s: req_alt_boot_device(0x%016llx)\n",
 490                            ioc->name, __func__,
 491                            (unsigned long long)sas_address));
 492                        ioc->req_alt_boot_device.device = device;
 493                        ioc->req_alt_boot_device.is_raid = is_raid;
 494                }
 495        }
 496
 497        if (!ioc->current_boot_device.device) {
 498                if (_scsih_is_boot_device(sas_address, device_name,
 499                    enclosure_logical_id, slot,
 500                    (ioc->bios_pg2.CurrentBootDeviceForm &
 501                    MPI2_BIOSPAGE2_FORM_MASK),
 502                    &ioc->bios_pg2.CurrentBootDevice)) {
 503                        dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
 504                           "%s: current_boot_device(0x%016llx)\n",
 505                            ioc->name, __func__,
 506                            (unsigned long long)sas_address));
 507                        ioc->current_boot_device.device = device;
 508                        ioc->current_boot_device.is_raid = is_raid;
 509                }
 510        }
 511}
 512
 513/**
 514 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
 515 * @ioc: per adapter object
 516 * @sas_address: sas address
 517 * Context: Calling function should acquire ioc->sas_device_lock
 518 *
 519 * This searches for sas_device based on sas_address, then return sas_device
 520 * object.
 521 */
 522struct _sas_device *
 523mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
 524    u64 sas_address)
 525{
 526        struct _sas_device *sas_device;
 527
 528        list_for_each_entry(sas_device, &ioc->sas_device_list, list)
 529                if (sas_device->sas_address == sas_address)
 530                        return sas_device;
 531
 532        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
 533                if (sas_device->sas_address == sas_address)
 534                        return sas_device;
 535
 536        return NULL;
 537}
 538
 539/**
 540 * _scsih_sas_device_find_by_handle - sas device search
 541 * @ioc: per adapter object
 542 * @handle: sas device handle (assigned by firmware)
 543 * Context: Calling function should acquire ioc->sas_device_lock
 544 *
 545 * This searches for sas_device based on sas_address, then return sas_device
 546 * object.
 547 */
 548static struct _sas_device *
 549_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 550{
 551        struct _sas_device *sas_device;
 552
 553        list_for_each_entry(sas_device, &ioc->sas_device_list, list)
 554                if (sas_device->handle == handle)
 555                        return sas_device;
 556
 557        list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
 558                if (sas_device->handle == handle)
 559                        return sas_device;
 560
 561        return NULL;
 562}
 563
 564/**
 565 * _scsih_sas_device_remove - remove sas_device from list.
 566 * @ioc: per adapter object
 567 * @sas_device: the sas_device object
 568 * Context: This function will acquire ioc->sas_device_lock.
 569 *
 570 * Removing object and freeing associated memory from the ioc->sas_device_list.
 571 */
 572static void
 573_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
 574    struct _sas_device *sas_device)
 575{
 576        unsigned long flags;
 577
 578        if (!sas_device)
 579                return;
 580
 581        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 582        if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
 583            sas_device->sas_address)) {
 584                list_del(&sas_device->list);
 585                kfree(sas_device);
 586        }
 587        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 588}
 589
 590/**
 591 * _scsih_sas_device_add - insert sas_device to the list.
 592 * @ioc: per adapter object
 593 * @sas_device: the sas_device object
 594 * Context: This function will acquire ioc->sas_device_lock.
 595 *
 596 * Adding new object to the ioc->sas_device_list.
 597 */
 598static void
 599_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
 600    struct _sas_device *sas_device)
 601{
 602        unsigned long flags;
 603
 604        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
 605            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
 606            sas_device->handle, (unsigned long long)sas_device->sas_address));
 607
 608        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 609        list_add_tail(&sas_device->list, &ioc->sas_device_list);
 610        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 611
 612        if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
 613             sas_device->sas_address_parent)) {
 614                _scsih_sas_device_remove(ioc, sas_device);
 615                } else if (!sas_device->starget) {
 616                        if (!ioc->is_driver_loading)
 617                                mpt2sas_transport_port_remove(ioc,
 618                                sas_device->sas_address,
 619                            sas_device->sas_address_parent);
 620                        _scsih_sas_device_remove(ioc, sas_device);
 621                }
 622}
 623
 624/**
 625 * _scsih_sas_device_init_add - insert sas_device to the list.
 626 * @ioc: per adapter object
 627 * @sas_device: the sas_device object
 628 * Context: This function will acquire ioc->sas_device_lock.
 629 *
 630 * Adding new object at driver load time to the ioc->sas_device_init_list.
 631 */
 632static void
 633_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
 634    struct _sas_device *sas_device)
 635{
 636        unsigned long flags;
 637
 638        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
 639            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
 640            sas_device->handle, (unsigned long long)sas_device->sas_address));
 641
 642        spin_lock_irqsave(&ioc->sas_device_lock, flags);
 643        list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
 644        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 645        _scsih_determine_boot_device(ioc, sas_device, 0);
 646}
 647
 648/**
 649 * _scsih_raid_device_find_by_id - raid device search
 650 * @ioc: per adapter object
 651 * @id: sas device target id
 652 * @channel: sas device channel
 653 * Context: Calling function should acquire ioc->raid_device_lock
 654 *
 655 * This searches for raid_device based on target id, then return raid_device
 656 * object.
 657 */
 658static struct _raid_device *
 659_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
 660{
 661        struct _raid_device *raid_device, *r;
 662
 663        r = NULL;
 664        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 665                if (raid_device->id == id && raid_device->channel == channel) {
 666                        r = raid_device;
 667                        goto out;
 668                }
 669        }
 670
 671 out:
 672        return r;
 673}
 674
 675/**
 676 * _scsih_raid_device_find_by_handle - raid device search
 677 * @ioc: per adapter object
 678 * @handle: sas device handle (assigned by firmware)
 679 * Context: Calling function should acquire ioc->raid_device_lock
 680 *
 681 * This searches for raid_device based on handle, then return raid_device
 682 * object.
 683 */
 684static struct _raid_device *
 685_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 686{
 687        struct _raid_device *raid_device, *r;
 688
 689        r = NULL;
 690        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 691                if (raid_device->handle != handle)
 692                        continue;
 693                r = raid_device;
 694                goto out;
 695        }
 696
 697 out:
 698        return r;
 699}
 700
 701/**
 702 * _scsih_raid_device_find_by_wwid - raid device search
 703 * @ioc: per adapter object
 704 * @handle: sas device handle (assigned by firmware)
 705 * Context: Calling function should acquire ioc->raid_device_lock
 706 *
 707 * This searches for raid_device based on wwid, then return raid_device
 708 * object.
 709 */
 710static struct _raid_device *
 711_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
 712{
 713        struct _raid_device *raid_device, *r;
 714
 715        r = NULL;
 716        list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
 717                if (raid_device->wwid != wwid)
 718                        continue;
 719                r = raid_device;
 720                goto out;
 721        }
 722
 723 out:
 724        return r;
 725}
 726
 727/**
 728 * _scsih_raid_device_add - add raid_device object
 729 * @ioc: per adapter object
 730 * @raid_device: raid_device object
 731 *
 732 * This is added to the raid_device_list link list.
 733 */
 734static void
 735_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
 736    struct _raid_device *raid_device)
 737{
 738        unsigned long flags;
 739
 740        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
 741            "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
 742            raid_device->handle, (unsigned long long)raid_device->wwid));
 743
 744        spin_lock_irqsave(&ioc->raid_device_lock, flags);
 745        list_add_tail(&raid_device->list, &ioc->raid_device_list);
 746        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
 747}
 748
 749/**
 750 * _scsih_raid_device_remove - delete raid_device object
 751 * @ioc: per adapter object
 752 * @raid_device: raid_device object
 753 *
 754 * This is removed from the raid_device_list link list.
 755 */
 756static void
 757_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
 758    struct _raid_device *raid_device)
 759{
 760        unsigned long flags;
 761
 762        spin_lock_irqsave(&ioc->raid_device_lock, flags);
 763        list_del(&raid_device->list);
 764        memset(raid_device, 0, sizeof(struct _raid_device));
 765        kfree(raid_device);
 766        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
 767}
 768
 769/**
 770 * mpt2sas_scsih_expander_find_by_handle - expander device search
 771 * @ioc: per adapter object
 772 * @handle: expander handle (assigned by firmware)
 773 * Context: Calling function should acquire ioc->sas_device_lock
 774 *
 775 * This searches for expander device based on handle, then returns the
 776 * sas_node object.
 777 */
 778struct _sas_node *
 779mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 780{
 781        struct _sas_node *sas_expander, *r;
 782
 783        r = NULL;
 784        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
 785                if (sas_expander->handle != handle)
 786                        continue;
 787                r = sas_expander;
 788                goto out;
 789        }
 790 out:
 791        return r;
 792}
 793
 794/**
 795 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
 796 * @ioc: per adapter object
 797 * @sas_address: sas address
 798 * Context: Calling function should acquire ioc->sas_node_lock.
 799 *
 800 * This searches for expander device based on sas_address, then returns the
 801 * sas_node object.
 802 */
 803struct _sas_node *
 804mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
 805    u64 sas_address)
 806{
 807        struct _sas_node *sas_expander, *r;
 808
 809        r = NULL;
 810        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
 811                if (sas_expander->sas_address != sas_address)
 812                        continue;
 813                r = sas_expander;
 814                goto out;
 815        }
 816 out:
 817        return r;
 818}
 819
 820/**
 821 * _scsih_expander_node_add - insert expander device to the list.
 822 * @ioc: per adapter object
 823 * @sas_expander: the sas_device object
 824 * Context: This function will acquire ioc->sas_node_lock.
 825 *
 826 * Adding new object to the ioc->sas_expander_list.
 827 *
 828 * Return nothing.
 829 */
 830static void
 831_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
 832    struct _sas_node *sas_expander)
 833{
 834        unsigned long flags;
 835
 836        spin_lock_irqsave(&ioc->sas_node_lock, flags);
 837        list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
 838        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
 839}
 840
 841/**
 842 * _scsih_is_end_device - determines if device is an end device
 843 * @device_info: bitfield providing information about the device.
 844 * Context: none
 845 *
 846 * Returns 1 if end device.
 847 */
 848static int
 849_scsih_is_end_device(u32 device_info)
 850{
 851        if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
 852                ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
 853                (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
 854                (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
 855                return 1;
 856        else
 857                return 0;
 858}
 859
 860/**
 861 * _scsih_scsi_lookup_get - returns scmd entry
 862 * @ioc: per adapter object
 863 * @smid: system request message index
 864 *
 865 * Returns the smid stored scmd pointer.
 866 */
 867static struct scsi_cmnd *
 868_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
 869{
 870        return ioc->scsi_lookup[smid - 1].scmd;
 871}
 872
 873/**
 874 * _scsih_scsi_lookup_get_clear - returns scmd entry
 875 * @ioc: per adapter object
 876 * @smid: system request message index
 877 *
 878 * Returns the smid stored scmd pointer.
 879 * Then will derefrence the stored scmd pointer.
 880 */
 881static inline struct scsi_cmnd *
 882_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
 883{
 884        unsigned long flags;
 885        struct scsi_cmnd *scmd;
 886
 887        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 888        scmd = ioc->scsi_lookup[smid - 1].scmd;
 889        ioc->scsi_lookup[smid - 1].scmd = NULL;
 890        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 891
 892        return scmd;
 893}
 894
 895/**
 896 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
 897 * @ioc: per adapter object
 898 * @smid: system request message index
 899 * @scmd: pointer to scsi command object
 900 * Context: This function will acquire ioc->scsi_lookup_lock.
 901 *
 902 * This will search for a scmd pointer in the scsi_lookup array,
 903 * returning the revelent smid.  A returned value of zero means invalid.
 904 */
 905static u16
 906_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
 907    *scmd)
 908{
 909        u16 smid;
 910        unsigned long   flags;
 911        int i;
 912
 913        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 914        smid = 0;
 915        for (i = 0; i < ioc->scsiio_depth; i++) {
 916                if (ioc->scsi_lookup[i].scmd == scmd) {
 917                        smid = ioc->scsi_lookup[i].smid;
 918                        goto out;
 919                }
 920        }
 921 out:
 922        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 923        return smid;
 924}
 925
 926/**
 927 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
 928 * @ioc: per adapter object
 929 * @id: target id
 930 * @channel: channel
 931 * Context: This function will acquire ioc->scsi_lookup_lock.
 932 *
 933 * This will search for a matching channel:id in the scsi_lookup array,
 934 * returning 1 if found.
 935 */
 936static u8
 937_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
 938    int channel)
 939{
 940        u8 found;
 941        unsigned long   flags;
 942        int i;
 943
 944        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 945        found = 0;
 946        for (i = 0 ; i < ioc->scsiio_depth; i++) {
 947                if (ioc->scsi_lookup[i].scmd &&
 948                    (ioc->scsi_lookup[i].scmd->device->id == id &&
 949                    ioc->scsi_lookup[i].scmd->device->channel == channel)) {
 950                        found = 1;
 951                        goto out;
 952                }
 953        }
 954 out:
 955        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 956        return found;
 957}
 958
 959/**
 960 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
 961 * @ioc: per adapter object
 962 * @id: target id
 963 * @lun: lun number
 964 * @channel: channel
 965 * Context: This function will acquire ioc->scsi_lookup_lock.
 966 *
 967 * This will search for a matching channel:id:lun in the scsi_lookup array,
 968 * returning 1 if found.
 969 */
 970static u8
 971_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
 972    unsigned int lun, int channel)
 973{
 974        u8 found;
 975        unsigned long   flags;
 976        int i;
 977
 978        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 979        found = 0;
 980        for (i = 0 ; i < ioc->scsiio_depth; i++) {
 981                if (ioc->scsi_lookup[i].scmd &&
 982                    (ioc->scsi_lookup[i].scmd->device->id == id &&
 983                    ioc->scsi_lookup[i].scmd->device->channel == channel &&
 984                    ioc->scsi_lookup[i].scmd->device->lun == lun)) {
 985                        found = 1;
 986                        goto out;
 987                }
 988        }
 989 out:
 990        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 991        return found;
 992}
 993
 994/**
 995 * _scsih_get_chain_buffer_tracker - obtain chain tracker
 996 * @ioc: per adapter object
 997 * @smid: smid associated to an IO request
 998 *
 999 * Returns chain tracker(from ioc->free_chain_list)
1000 */
1001static struct chain_tracker *
1002_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1003{
1004        struct chain_tracker *chain_req;
1005        unsigned long flags;
1006
1007        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1008        if (list_empty(&ioc->free_chain_list)) {
1009                spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1010                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1011                        "available\n", ioc->name));
1012                return NULL;
1013        }
1014        chain_req = list_entry(ioc->free_chain_list.next,
1015            struct chain_tracker, tracker_list);
1016        list_del_init(&chain_req->tracker_list);
1017        list_add_tail(&chain_req->tracker_list,
1018            &ioc->scsi_lookup[smid - 1].chain_list);
1019        spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1020        return chain_req;
1021}
1022
1023/**
1024 * _scsih_build_scatter_gather - main sg creation routine
1025 * @ioc: per adapter object
1026 * @scmd: scsi command
1027 * @smid: system request message index
1028 * Context: none.
1029 *
1030 * The main routine that builds scatter gather table from a given
1031 * scsi request sent via the .queuecommand main handler.
1032 *
1033 * Returns 0 success, anything else error
1034 */
1035static int
1036_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1037    struct scsi_cmnd *scmd, u16 smid)
1038{
1039        Mpi2SCSIIORequest_t *mpi_request;
1040        dma_addr_t chain_dma;
1041        struct scatterlist *sg_scmd;
1042        void *sg_local, *chain;
1043        u32 chain_offset;
1044        u32 chain_length;
1045        u32 chain_flags;
1046        int sges_left;
1047        u32 sges_in_segment;
1048        u32 sgl_flags;
1049        u32 sgl_flags_last_element;
1050        u32 sgl_flags_end_buffer;
1051        struct chain_tracker *chain_req;
1052
1053        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1054
1055        /* init scatter gather flags */
1056        sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1057        if (scmd->sc_data_direction == DMA_TO_DEVICE)
1058                sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1059        sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1060            << MPI2_SGE_FLAGS_SHIFT;
1061        sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1062            MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1063            << MPI2_SGE_FLAGS_SHIFT;
1064        sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1065
1066        sg_scmd = scsi_sglist(scmd);
1067        sges_left = scsi_dma_map(scmd);
1068        if (sges_left < 0) {
1069                sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1070                " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1071                return -ENOMEM;
1072        }
1073
1074        sg_local = &mpi_request->SGL;
1075        sges_in_segment = ioc->max_sges_in_main_message;
1076        if (sges_left <= sges_in_segment)
1077                goto fill_in_last_segment;
1078
1079        mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1080            (sges_in_segment * ioc->sge_size))/4;
1081
1082        /* fill in main message segment when there is a chain following */
1083        while (sges_in_segment) {
1084                if (sges_in_segment == 1)
1085                        ioc->base_add_sg_single(sg_local,
1086                            sgl_flags_last_element | sg_dma_len(sg_scmd),
1087                            sg_dma_address(sg_scmd));
1088                else
1089                        ioc->base_add_sg_single(sg_local, sgl_flags |
1090                            sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1091                sg_scmd = sg_next(sg_scmd);
1092                sg_local += ioc->sge_size;
1093                sges_left--;
1094                sges_in_segment--;
1095        }
1096
1097        /* initializing the chain flags and pointers */
1098        chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1099        chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1100        if (!chain_req)
1101                return -1;
1102        chain = chain_req->chain_buffer;
1103        chain_dma = chain_req->chain_buffer_dma;
1104        do {
1105                sges_in_segment = (sges_left <=
1106                    ioc->max_sges_in_chain_message) ? sges_left :
1107                    ioc->max_sges_in_chain_message;
1108                chain_offset = (sges_left == sges_in_segment) ?
1109                    0 : (sges_in_segment * ioc->sge_size)/4;
1110                chain_length = sges_in_segment * ioc->sge_size;
1111                if (chain_offset) {
1112                        chain_offset = chain_offset <<
1113                            MPI2_SGE_CHAIN_OFFSET_SHIFT;
1114                        chain_length += ioc->sge_size;
1115                }
1116                ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1117                    chain_length, chain_dma);
1118                sg_local = chain;
1119                if (!chain_offset)
1120                        goto fill_in_last_segment;
1121
1122                /* fill in chain segments */
1123                while (sges_in_segment) {
1124                        if (sges_in_segment == 1)
1125                                ioc->base_add_sg_single(sg_local,
1126                                    sgl_flags_last_element |
1127                                    sg_dma_len(sg_scmd),
1128                                    sg_dma_address(sg_scmd));
1129                        else
1130                                ioc->base_add_sg_single(sg_local, sgl_flags |
1131                                    sg_dma_len(sg_scmd),
1132                                    sg_dma_address(sg_scmd));
1133                        sg_scmd = sg_next(sg_scmd);
1134                        sg_local += ioc->sge_size;
1135                        sges_left--;
1136                        sges_in_segment--;
1137                }
1138
1139                chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1140                if (!chain_req)
1141                        return -1;
1142                chain = chain_req->chain_buffer;
1143                chain_dma = chain_req->chain_buffer_dma;
1144        } while (1);
1145
1146
1147 fill_in_last_segment:
1148
1149        /* fill the last segment */
1150        while (sges_left) {
1151                if (sges_left == 1)
1152                        ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1153                            sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1154                else
1155                        ioc->base_add_sg_single(sg_local, sgl_flags |
1156                            sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1157                sg_scmd = sg_next(sg_scmd);
1158                sg_local += ioc->sge_size;
1159                sges_left--;
1160        }
1161
1162        return 0;
1163}
1164
1165/**
1166 * _scsih_adjust_queue_depth - setting device queue depth
1167 * @sdev: scsi device struct
1168 * @qdepth: requested queue depth
1169 *
1170 *
1171 * Returns nothing
1172 */
1173static void
1174_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
1175{
1176        struct Scsi_Host *shost = sdev->host;
1177        int max_depth;
1178        struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1179        struct MPT2SAS_DEVICE *sas_device_priv_data;
1180        struct MPT2SAS_TARGET *sas_target_priv_data;
1181        struct _sas_device *sas_device;
1182        unsigned long flags;
1183
1184        max_depth = shost->can_queue;
1185
1186        /* limit max device queue for SATA to 32 */
1187        sas_device_priv_data = sdev->hostdata;
1188        if (!sas_device_priv_data)
1189                goto not_sata;
1190        sas_target_priv_data = sas_device_priv_data->sas_target;
1191        if (!sas_target_priv_data)
1192                goto not_sata;
1193        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1194                goto not_sata;
1195        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1196        sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1197           sas_device_priv_data->sas_target->sas_address);
1198        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1199        if (sas_device && sas_device->device_info &
1200            MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1201                max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1202
1203 not_sata:
1204
1205        if (!sdev->tagged_supported)
1206                max_depth = 1;
1207        if (qdepth > max_depth)
1208                qdepth = max_depth;
1209        scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1210}
1211
1212/**
1213 * _scsih_change_queue_depth - setting device queue depth
1214 * @sdev: scsi device struct
1215 * @qdepth: requested queue depth
1216 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1217 * (see include/scsi/scsi_host.h for definition)
1218 *
1219 * Returns queue depth.
1220 */
1221static int
1222_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1223{
1224        if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1225                _scsih_adjust_queue_depth(sdev, qdepth);
1226        else if (reason == SCSI_QDEPTH_QFULL)
1227                scsi_track_queue_full(sdev, qdepth);
1228        else
1229                return -EOPNOTSUPP;
1230
1231        if (sdev->inquiry_len > 7)
1232                sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1233                "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1234                sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1235                sdev->ordered_tags, sdev->scsi_level,
1236                (sdev->inquiry[7] & 2) >> 1);
1237
1238        return sdev->queue_depth;
1239}
1240
1241/**
1242 * _scsih_change_queue_type - changing device queue tag type
1243 * @sdev: scsi device struct
1244 * @tag_type: requested tag type
1245 *
1246 * Returns queue tag type.
1247 */
1248static int
1249_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1250{
1251        if (sdev->tagged_supported) {
1252                scsi_set_tag_type(sdev, tag_type);
1253                if (tag_type)
1254                        scsi_activate_tcq(sdev, sdev->queue_depth);
1255                else
1256                        scsi_deactivate_tcq(sdev, sdev->queue_depth);
1257        } else
1258                tag_type = 0;
1259
1260        return tag_type;
1261}
1262
1263/**
1264 * _scsih_target_alloc - target add routine
1265 * @starget: scsi target struct
1266 *
1267 * Returns 0 if ok. Any other return is assumed to be an error and
1268 * the device is ignored.
1269 */
1270static int
1271_scsih_target_alloc(struct scsi_target *starget)
1272{
1273        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1274        struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1275        struct MPT2SAS_TARGET *sas_target_priv_data;
1276        struct _sas_device *sas_device;
1277        struct _raid_device *raid_device;
1278        unsigned long flags;
1279        struct sas_rphy *rphy;
1280
1281        sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1282        if (!sas_target_priv_data)
1283                return -ENOMEM;
1284
1285        starget->hostdata = sas_target_priv_data;
1286        sas_target_priv_data->starget = starget;
1287        sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1288
1289        /* RAID volumes */
1290        if (starget->channel == RAID_CHANNEL) {
1291                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1292                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1293                    starget->channel);
1294                if (raid_device) {
1295                        sas_target_priv_data->handle = raid_device->handle;
1296                        sas_target_priv_data->sas_address = raid_device->wwid;
1297                        sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1298                        sas_target_priv_data->raid_device = raid_device;
1299                        raid_device->starget = starget;
1300                }
1301                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1302                return 0;
1303        }
1304
1305        /* sas/sata devices */
1306        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1307        rphy = dev_to_rphy(starget->dev.parent);
1308        sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1309           rphy->identify.sas_address);
1310
1311        if (sas_device) {
1312                sas_target_priv_data->handle = sas_device->handle;
1313                sas_target_priv_data->sas_address = sas_device->sas_address;
1314                sas_device->starget = starget;
1315                sas_device->id = starget->id;
1316                sas_device->channel = starget->channel;
1317                if (test_bit(sas_device->handle, ioc->pd_handles))
1318                        sas_target_priv_data->flags |=
1319                            MPT_TARGET_FLAGS_RAID_COMPONENT;
1320        }
1321        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1322
1323        return 0;
1324}
1325
1326/**
1327 * _scsih_target_destroy - target destroy routine
1328 * @starget: scsi target struct
1329 *
1330 * Returns nothing.
1331 */
1332static void
1333_scsih_target_destroy(struct scsi_target *starget)
1334{
1335        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1336        struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1337        struct MPT2SAS_TARGET *sas_target_priv_data;
1338        struct _sas_device *sas_device;
1339        struct _raid_device *raid_device;
1340        unsigned long flags;
1341        struct sas_rphy *rphy;
1342
1343        sas_target_priv_data = starget->hostdata;
1344        if (!sas_target_priv_data)
1345                return;
1346
1347        if (starget->channel == RAID_CHANNEL) {
1348                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1349                raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1350                    starget->channel);
1351                if (raid_device) {
1352                        raid_device->starget = NULL;
1353                        raid_device->sdev = NULL;
1354                }
1355                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1356                goto out;
1357        }
1358
1359        spin_lock_irqsave(&ioc->sas_device_lock, flags);
1360        rphy = dev_to_rphy(starget->dev.parent);
1361        sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1362           rphy->identify.sas_address);
1363        if (sas_device && (sas_device->starget == starget) &&
1364            (sas_device->id == starget->id) &&
1365            (sas_device->channel == starget->channel))
1366                sas_device->starget = NULL;
1367
1368        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1369
1370 out:
1371        kfree(sas_target_priv_data);
1372        starget->hostdata = NULL;
1373}
1374
1375/**
1376 * _scsih_slave_alloc - device add routine
1377 * @sdev: scsi device struct
1378 *
1379 * Returns 0 if ok. Any other return is assumed to be an error and
1380 * the device is ignored.
1381 */
1382static int
1383_scsih_slave_alloc(struct scsi_device *sdev)
1384{
1385        struct Scsi_Host *shost;
1386        struct MPT2SAS_ADAPTER *ioc;
1387        struct MPT2SAS_TARGET *sas_target_priv_data;
1388        struct MPT2SAS_DEVICE *sas_device_priv_data;
1389        struct scsi_target *starget;
1390        struct _raid_device *raid_device;
1391        unsigned long flags;
1392
1393        sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1394        if (!sas_device_priv_data)
1395                return -ENOMEM;
1396
1397        sas_device_priv_data->lun = sdev->lun;
1398        sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1399
1400        starget = scsi_target(sdev);
1401        sas_target_priv_data = starget->hostdata;
1402        sas_target_priv_data->num_luns++;
1403        sas_device_priv_data->sas_target = sas_target_priv_data;
1404        sdev->hostdata = sas_device_priv_data;
1405        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1406                sdev->no_uld_attach = 1;
1407
1408        shost = dev_to_shost(&starget->dev);
1409        ioc = shost_priv(shost);
1410        if (starget->channel == RAID_CHANNEL) {
1411                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1412                raid_device = _scsih_raid_device_find_by_id(ioc,
1413                    starget->id, starget->channel);
1414                if (raid_device)
1415                        raid_device->sdev = sdev; /* raid is single lun */
1416                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1417        }
1418
1419        return 0;
1420}
1421
1422/**
1423 * _scsih_slave_destroy - device destroy routine
1424 * @sdev: scsi device struct
1425 *
1426 * Returns nothing.
1427 */
1428static void
1429_scsih_slave_destroy(struct scsi_device *sdev)
1430{
1431        struct MPT2SAS_TARGET *sas_target_priv_data;
1432        struct scsi_target *starget;
1433        struct Scsi_Host *shost;
1434        struct MPT2SAS_ADAPTER *ioc;
1435        struct _sas_device *sas_device;
1436        unsigned long flags;
1437
1438        if (!sdev->hostdata)
1439                return;
1440
1441        starget = scsi_target(sdev);
1442        sas_target_priv_data = starget->hostdata;
1443        sas_target_priv_data->num_luns--;
1444
1445        shost = dev_to_shost(&starget->dev);
1446        ioc = shost_priv(shost);
1447
1448        if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1449                spin_lock_irqsave(&ioc->sas_device_lock, flags);
1450                sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1451                   sas_target_priv_data->sas_address);
1452                if (sas_device)
1453                        sas_device->starget = NULL;
1454                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1455        }
1456
1457        kfree(sdev->hostdata);
1458        sdev->hostdata = NULL;
1459}
1460
1461/**
1462 * _scsih_display_sata_capabilities - sata capabilities
1463 * @ioc: per adapter object
1464 * @sas_device: the sas_device object
1465 * @sdev: scsi device struct
1466 */
1467static void
1468_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1469    struct _sas_device *sas_device, struct scsi_device *sdev)
1470{
1471        Mpi2ConfigReply_t mpi_reply;
1472        Mpi2SasDevicePage0_t sas_device_pg0;
1473        u32 ioc_status;
1474        u16 flags;
1475        u32 device_info;
1476
1477        if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1478            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1479                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1480                    ioc->name, __FILE__, __LINE__, __func__);
1481                return;
1482        }
1483
1484        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1485            MPI2_IOCSTATUS_MASK;
1486        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1487                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488                    ioc->name, __FILE__, __LINE__, __func__);
1489                return;
1490        }
1491
1492        flags = le16_to_cpu(sas_device_pg0.Flags);
1493        device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1494
1495        sdev_printk(KERN_INFO, sdev,
1496            "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1497            "sw_preserve(%s)\n",
1498            (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1499            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1500            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1501            "n",
1502            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1503            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1504            (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1505}
1506
1507/**
1508 * _scsih_is_raid - return boolean indicating device is raid volume
1509 * @dev the device struct object
1510 */
1511static int
1512_scsih_is_raid(struct device *dev)
1513{
1514        struct scsi_device *sdev = to_scsi_device(dev);
1515        struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1516
1517        if (ioc->is_warpdrive)
1518                return 0;
1519        return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1520}
1521
1522/**
1523 * _scsih_get_resync - get raid volume resync percent complete
1524 * @dev the device struct object
1525 */
1526static void
1527_scsih_get_resync(struct device *dev)
1528{
1529        struct scsi_device *sdev = to_scsi_device(dev);
1530        struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1531        static struct _raid_device *raid_device;
1532        unsigned long flags;
1533        Mpi2RaidVolPage0_t vol_pg0;
1534        Mpi2ConfigReply_t mpi_reply;
1535        u32 volume_status_flags;
1536        u8 percent_complete = 0;
1537
1538        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1539        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1540            sdev->channel);
1541        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1542
1543        if (!raid_device || ioc->is_warpdrive)
1544                goto out;
1545
1546        if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1547             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1548             sizeof(Mpi2RaidVolPage0_t))) {
1549                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1550                    ioc->name, __FILE__, __LINE__, __func__);
1551                goto out;
1552        }
1553
1554        volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1555        if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1556                percent_complete = raid_device->percent_complete;
1557 out:
1558        raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1559}
1560
1561/**
1562 * _scsih_get_state - get raid volume level
1563 * @dev the device struct object
1564 */
1565static void
1566_scsih_get_state(struct device *dev)
1567{
1568        struct scsi_device *sdev = to_scsi_device(dev);
1569        struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1570        static struct _raid_device *raid_device;
1571        unsigned long flags;
1572        Mpi2RaidVolPage0_t vol_pg0;
1573        Mpi2ConfigReply_t mpi_reply;
1574        u32 volstate;
1575        enum raid_state state = RAID_STATE_UNKNOWN;
1576
1577        spin_lock_irqsave(&ioc->raid_device_lock, flags);
1578        raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1579            sdev->channel);
1580        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1581
1582        if (!raid_device)
1583                goto out;
1584
1585        if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1586             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1587             sizeof(Mpi2RaidVolPage0_t))) {
1588                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1589                    ioc->name, __FILE__, __LINE__, __func__);
1590                goto out;
1591        }
1592
1593        volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1594        if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1595                state = RAID_STATE_RESYNCING;
1596                goto out;
1597        }
1598
1599        switch (vol_pg0.VolumeState) {
1600        case MPI2_RAID_VOL_STATE_OPTIMAL:
1601        case MPI2_RAID_VOL_STATE_ONLINE:
1602                state = RAID_STATE_ACTIVE;
1603                break;
1604        case  MPI2_RAID_VOL_STATE_DEGRADED:
1605                state = RAID_STATE_DEGRADED;
1606                break;
1607        case MPI2_RAID_VOL_STATE_FAILED:
1608        case MPI2_RAID_VOL_STATE_MISSING:
1609                state = RAID_STATE_OFFLINE;
1610                break;
1611        }
1612 out:
1613        raid_set_state(mpt2sas_raid_template, dev, state);
1614}
1615
1616/**
1617 * _scsih_set_level - set raid level
1618 * @sdev: scsi device struct
1619 * @raid_device: raid_device object
1620 */
1621static void
1622_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1623{
1624        enum raid_level level = RAID_LEVEL_UNKNOWN;
1625
1626        switch (raid_device->volume_type) {
1627        case MPI2_RAID_VOL_TYPE_RAID0:
1628                level = RAID_LEVEL_0;
1629                break;
1630        case MPI2_RAID_VOL_TYPE_RAID10:
1631                level = RAID_LEVEL_10;
1632                break;
1633        case MPI2_RAID_VOL_TYPE_RAID1E:
1634                level = RAID_LEVEL_1E;
1635                break;
1636        case MPI2_RAID_VOL_TYPE_RAID1:
1637                level = RAID_LEVEL_1;
1638                break;
1639        }
1640
1641        raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1642}
1643
1644/**
1645 * _scsih_get_volume_capabilities - volume capabilities
1646 * @ioc: per adapter object
1647 * @sas_device: the raid_device object
1648 *
1649 * Returns 0 for success, else 1
1650 */
1651static int
1652_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1653    struct _raid_device *raid_device)
1654{
1655        Mpi2RaidVolPage0_t *vol_pg0;
1656        Mpi2RaidPhysDiskPage0_t pd_pg0;
1657        Mpi2SasDevicePage0_t sas_device_pg0;
1658        Mpi2ConfigReply_t mpi_reply;
1659        u16 sz;
1660        u8 num_pds;
1661
1662        if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1663            &num_pds)) || !num_pds) {
1664                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1665                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1666                    __func__));
1667                return 1;
1668        }
1669
1670        raid_device->num_pds = num_pds;
1671        sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1672            sizeof(Mpi2RaidVol0PhysDisk_t));
1673        vol_pg0 = kzalloc(sz, GFP_KERNEL);
1674        if (!vol_pg0) {
1675                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1676                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1677                    __func__));
1678                return 1;
1679        }
1680
1681        if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1682             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1683                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1684                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1685                    __func__));
1686                kfree(vol_pg0);
1687                return 1;
1688        }
1689
1690        raid_device->volume_type = vol_pg0->VolumeType;
1691
1692        /* figure out what the underlying devices are by
1693         * obtaining the device_info bits for the 1st device
1694         */
1695        if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1696            &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1697            vol_pg0->PhysDisk[0].PhysDiskNum))) {
1698                if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1699                    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1700                    le16_to_cpu(pd_pg0.DevHandle)))) {
1701                        raid_device->device_info =
1702                            le32_to_cpu(sas_device_pg0.DeviceInfo);
1703                }
1704        }
1705
1706        kfree(vol_pg0);
1707        return 0;
1708}
1709/**
1710 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1711 * @ioc: per adapter object
1712 */
1713static void
1714_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1715{
1716        Mpi2RaidVolPage1_t vol_pg1;
1717        Mpi2ConfigReply_t mpi_reply;
1718        struct _raid_device *raid_device;
1719        u16 handle;
1720        u16 ioc_status;
1721
1722        handle = 0xFFFF;
1723        while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1724            &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1725                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1726                    MPI2_IOCSTATUS_MASK;
1727                if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1728                        break;
1729                handle = le16_to_cpu(vol_pg1.DevHandle);
1730                raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1731                if (raid_device)
1732                        raid_device->direct_io_enabled = 0;
1733        }
1734        return;
1735}
1736
1737
1738/**
1739 * _scsih_get_num_volumes - Get number of volumes in the ioc
1740 * @ioc: per adapter object
1741 */
1742static u8
1743_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1744{
1745        Mpi2RaidVolPage1_t vol_pg1;
1746        Mpi2ConfigReply_t mpi_reply;
1747        u16 handle;
1748        u8 vol_cnt = 0;
1749        u16 ioc_status;
1750
1751        handle = 0xFFFF;
1752        while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1753            &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1754                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1755                    MPI2_IOCSTATUS_MASK;
1756                if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1757                        break;
1758                vol_cnt++;
1759                handle = le16_to_cpu(vol_pg1.DevHandle);
1760        }
1761        return vol_cnt;
1762}
1763
1764
1765/**
1766 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1767 * @ioc: per adapter object
1768 * @raid_device: the raid_device object
1769 */
1770static void
1771_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1772        struct _raid_device *raid_device)
1773{
1774        Mpi2RaidVolPage0_t *vol_pg0;
1775        Mpi2RaidPhysDiskPage0_t pd_pg0;
1776        Mpi2ConfigReply_t mpi_reply;
1777        u16 sz;
1778        u8 num_pds, count;
1779        u64 mb = 1024 * 1024;
1780        u64 tb_2 = 2 * mb * mb;
1781        u64 capacity;
1782        u32 stripe_sz;
1783        u8 i, stripe_exp;
1784
1785        if (!ioc->is_warpdrive)
1786                return;
1787
1788        if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS) {
1789                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1790                    "globally as drives are exposed\n", ioc->name);
1791                return;
1792        }
1793        if (_scsih_get_num_volumes(ioc) > 1) {
1794                _scsih_disable_ddio(ioc);
1795                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1796                    "globally as number of drives > 1\n", ioc->name);
1797                return;
1798        }
1799        if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1800            &num_pds)) || !num_pds) {
1801                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1802                    "Failure in computing number of drives\n", ioc->name);
1803                return;
1804        }
1805
1806        sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1807            sizeof(Mpi2RaidVol0PhysDisk_t));
1808        vol_pg0 = kzalloc(sz, GFP_KERNEL);
1809        if (!vol_pg0) {
1810                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1811                    "Memory allocation failure for RVPG0\n", ioc->name);
1812                return;
1813        }
1814
1815        if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1816             MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1817                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818                    "Failure in retrieving RVPG0\n", ioc->name);
1819                kfree(vol_pg0);
1820                return;
1821        }
1822
1823        /*
1824         * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1825         * assumed for WARPDRIVE, disable direct I/O
1826         */
1827        if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1828                printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1829                    "for the drive with handle(0x%04x): num_mem=%d, "
1830                    "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1831                    num_pds, MPT_MAX_WARPDRIVE_PDS);
1832                kfree(vol_pg0);
1833                return;
1834        }
1835        for (count = 0; count < num_pds; count++) {
1836                if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1837                    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1838                    vol_pg0->PhysDisk[count].PhysDiskNum) ||
1839                    pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1840                        printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1841                            "disabled for the drive with handle(0x%04x) member"
1842                            "handle retrieval failed for member number=%d\n",
1843                            ioc->name, raid_device->handle,
1844                            vol_pg0->PhysDisk[count].PhysDiskNum);
1845                        goto out_error;
1846                }
1847                raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1848        }
1849
1850        /*
1851         * Assumption for WD: Direct I/O is not supported if the volume is
1852         * not RAID0, if the stripe size is not 64KB, if the block size is
1853         * not 512 and if the volume size is >2TB
1854         */
1855        if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1856            le16_to_cpu(vol_pg0->BlockSize) != 512) {
1857                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1858                    "for the drive with handle(0x%04x): type=%d, "
1859                    "s_sz=%uK, blk_size=%u\n", ioc->name,
1860                    raid_device->handle, raid_device->volume_type,
1861                    le32_to_cpu(vol_pg0->StripeSize)/2,
1862                    le16_to_cpu(vol_pg0->BlockSize));
1863                goto out_error;
1864        }
1865
1866        capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1867            (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1868
1869        if (capacity > tb_2) {
1870                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1871                "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1872                ioc->name, raid_device->handle);
1873                goto out_error;
1874        }
1875
1876        stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1877        stripe_exp = 0;
1878        for (i = 0; i < 32; i++) {
1879                if (stripe_sz & 1)
1880                        break;
1881                stripe_exp++;
1882                stripe_sz >>= 1;
1883        }
1884        if (i == 32) {
1885                printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1886                    "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1887                    ioc->name, raid_device->handle,
1888                    le32_to_cpu(vol_pg0->StripeSize)/2);
1889                goto out_error;
1890        }
1891        raid_device->stripe_exponent = stripe_exp;
1892        raid_device->direct_io_enabled = 1;
1893
1894        printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1895            " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1896        /*
1897         * WARPDRIVE: Though the following fields are not used for direct IO,
1898         * stored for future purpose:
1899         */
1900        raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1901        raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1902        raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1903
1904
1905        kfree(vol_pg0);
1906        return;
1907
1908out_error:
1909        raid_device->direct_io_enabled = 0;
1910        for (count = 0; count < num_pds; count++)
1911                raid_device->pd_handle[count] = 0;
1912        kfree(vol_pg0);
1913        return;
1914}
1915
1916/**
1917 * _scsih_enable_tlr - setting TLR flags
1918 * @ioc: per adapter object
1919 * @sdev: scsi device struct
1920 *
1921 * Enabling Transaction Layer Retries for tape devices when
1922 * vpd page 0x90 is present
1923 *
1924 */
1925static void
1926_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1927{
1928        /* only for TAPE */
1929        if (sdev->type != TYPE_TAPE)
1930                return;
1931
1932        if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1933                return;
1934
1935        sas_enable_tlr(sdev);
1936        sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1937            sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1938        return;
1939
1940}
1941
1942/**
1943 * _scsih_slave_configure - device configure routine.
1944 * @sdev: scsi device struct
1945 *
1946 * Returns 0 if ok. Any other return is assumed to be an error and
1947 * the device is ignored.
1948 */
1949static int
1950_scsih_slave_configure(struct scsi_device *sdev)
1951{
1952        struct Scsi_Host *shost = sdev->host;
1953        struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1954        struct MPT2SAS_DEVICE *sas_device_priv_data;
1955        struct MPT2SAS_TARGET *sas_target_priv_data;
1956        struct _sas_device *sas_device;
1957        struct _raid_device *raid_device;
1958        unsigned long flags;
1959        int qdepth;
1960        u8 ssp_target = 0;
1961        char *ds = "";
1962        char *r_level = "";
1963
1964        qdepth = 1;
1965        sas_device_priv_data = sdev->hostdata;
1966        sas_device_priv_data->configured_lun = 1;
1967        sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1968        sas_target_priv_data = sas_device_priv_data->sas_target;
1969
1970        /* raid volume handling */
1971        if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1972
1973                spin_lock_irqsave(&ioc->raid_device_lock, flags);
1974                raid_device = _scsih_raid_device_find_by_handle(ioc,
1975                     sas_target_priv_data->handle);
1976                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1977                if (!raid_device) {
1978                        dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1979                            "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1980                            __LINE__, __func__));
1981                        return 1;
1982                }
1983
1984                _scsih_get_volume_capabilities(ioc, raid_device);
1985
1986                if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1987                        dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1988                            "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1989                            __LINE__, __func__));
1990                        return 1;
1991                }
1992                /*
1993                 * WARPDRIVE: Initialize the required data for Direct IO
1994                 */
1995                _scsih_init_warpdrive_properties(ioc, raid_device);
1996
1997                /* RAID Queue Depth Support
1998                 * IS volume = underlying qdepth of drive type, either
1999                 *    MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2000                 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2001                 */
2002                if (raid_device->device_info &
2003                    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2004                        qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2005                        ds = "SSP";
2006                } else {
2007                        qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2008                         if (raid_device->device_info &
2009                            MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2010                                ds = "SATA";
2011                        else
2012                                ds = "STP";
2013                }
2014
2015                switch (raid_device->volume_type) {
2016                case MPI2_RAID_VOL_TYPE_RAID0:
2017                        r_level = "RAID0";
2018                        break;
2019                case MPI2_RAID_VOL_TYPE_RAID1E:
2020                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2021                        if (ioc->manu_pg10.OEMIdentifier &&
2022                            (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
2023                            MFG10_GF0_R10_DISPLAY) &&
2024                            !(raid_device->num_pds % 2))
2025                                r_level = "RAID10";
2026                        else
2027                                r_level = "RAID1E";
2028                        break;
2029                case MPI2_RAID_VOL_TYPE_RAID1:
2030                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2031                        r_level = "RAID1";
2032                        break;
2033                case MPI2_RAID_VOL_TYPE_RAID10:
2034                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035                        r_level = "RAID10";
2036                        break;
2037                case MPI2_RAID_VOL_TYPE_UNKNOWN:
2038                default:
2039                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2040                        r_level = "RAIDX";
2041                        break;
2042                }
2043
2044                if (!ioc->hide_ir_msg)
2045                        sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2046                            "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2047                            r_level, raid_device->handle,
2048                            (unsigned long long)raid_device->wwid,
2049                            raid_device->num_pds, ds);
2050                _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
2051                /* raid transport support */
2052                if (!ioc->is_warpdrive)
2053                        _scsih_set_level(sdev, raid_device);
2054                return 0;
2055        }
2056
2057        /* non-raid handling */
2058        spin_lock_irqsave(&ioc->sas_device_lock, flags);
2059        sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2060           sas_device_priv_data->sas_target->sas_address);
2061        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2062        if (sas_device) {
2063                if (sas_target_priv_data->flags &
2064                    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2065                        if (mpt2sas_config_get_volume_handle(ioc,
2066                            sas_device->handle, &sas_device->volume_handle)) {
2067                                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2068                                    "failure at %s:%d/%s()!\n", ioc->name,
2069                                    __FILE__, __LINE__, __func__));
2070                                return 1;
2071                        }
2072                        if (sas_device->volume_handle &&
2073                            mpt2sas_config_get_volume_wwid(ioc,
2074                            sas_device->volume_handle,
2075                            &sas_device->volume_wwid)) {
2076                                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2077                                    "failure at %s:%d/%s()!\n", ioc->name,
2078                                    __FILE__, __LINE__, __func__));
2079                                return 1;
2080                        }
2081                }
2082                if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2083                        qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2084                        ssp_target = 1;
2085                        ds = "SSP";
2086                } else {
2087                        qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2088                        if (sas_device->device_info &
2089                            MPI2_SAS_DEVICE_INFO_STP_TARGET)
2090                                ds = "STP";
2091                        else if (sas_device->device_info &
2092                            MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2093                                ds = "SATA";
2094                }
2095
2096                sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2097                    "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2098                    ds, sas_device->handle,
2099                    (unsigned long long)sas_device->sas_address,
2100                    sas_device->phy,
2101                    (unsigned long long)sas_device->device_name);
2102                sdev_printk(KERN_INFO, sdev, "%s: "
2103                    "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2104                    (unsigned long long) sas_device->enclosure_logical_id,
2105                    sas_device->slot);
2106
2107                if (!ssp_target)
2108                        _scsih_display_sata_capabilities(ioc, sas_device, sdev);
2109        } else {
2110                dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2111                    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2112                    __func__));
2113                return 1;
2114        }
2115
2116        _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
2117
2118        if (ssp_target) {
2119                sas_read_port_mode_page(sdev);
2120                _scsih_enable_tlr(ioc, sdev);
2121        }
2122        return 0;
2123}
2124
2125/**
2126 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
2127 * @sdev: scsi device struct
2128 * @bdev: pointer to block device context
2129 * @capacity: device size (in 512 byte sectors)
2130 * @params: three element array to place output:
2131 *              params[0] number of heads (max 255)
2132 *              params[1] number of sectors (max 63)
2133 *              params[2] number of cylinders
2134 *
2135 * Return nothing.
2136 */
2137static int
2138_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2139    sector_t capacity, int params[])
2140{
2141        int             heads;
2142        int             sectors;
2143        sector_t        cylinders;
2144        ulong           dummy;
2145
2146        heads = 64;
2147        sectors = 32;
2148
2149        dummy = heads * sectors;
2150        cylinders = capacity;
2151        sector_div(cylinders, dummy);
2152
2153        /*
2154         * Handle extended translation size for logical drives
2155         * > 1Gb
2156         */
2157        if ((ulong)capacity >= 0x200000) {
2158                heads = 255;
2159                sectors = 63;
2160                dummy = heads * sectors;
2161                cylinders = capacity;
2162                sector_div(cylinders, dummy);
2163        }
2164
2165        /* return result */
2166        params[0] = heads;
2167        params[1] = sectors;
2168        params[2] = cylinders;
2169
2170        return 0;
2171}
2172
2173/**
2174 * _scsih_response_code - translation of device response code
2175 * @ioc: per adapter object
2176 * @response_code: response code returned by the device
2177 *
2178 * Return nothing.
2179 */
2180static void
2181_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2182{
2183        char *desc;
2184
2185        switch (response_code) {
2186        case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2187                desc = "task management request completed";
2188                break;
2189        case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2190                desc = "invalid frame";
2191                break;
2192        case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2193                desc = "task management request not supported";
2194                break;
2195        case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2196                desc = "task management request failed";
2197                break;
2198        case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2199                desc = "task management request succeeded";
2200                break;
2201        case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2202                desc = "invalid lun";
2203                break;
2204        case 0xA:
2205                desc = "overlapped tag attempted";
2206                break;
2207        case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2208                desc = "task queued, however not sent to target";
2209                break;
2210        default:
2211                desc = "unknown";
2212                break;
2213        }
2214        printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2215                ioc->name, response_code, desc);
2216}
2217
2218/**
2219 * _scsih_tm_done - tm completion routine
2220 * @ioc: per adapter object
2221 * @smid: system request message index
2222 * @msix_index: MSIX table index supplied by the OS
2223 * @reply: reply message frame(lower 32bit addr)
2224 * Context: none.
2225 *
2226 * The callback handler when using scsih_issue_tm.
2227 *
2228 * Return 1 meaning mf should be freed from _base_interrupt
2229 *        0 means the mf is freed from this function.
2230 */
2231static u8
2232_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2233{
2234        MPI2DefaultReply_t *mpi_reply;
2235
2236        if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
2237                return 1;
2238        if (ioc->tm_cmds.smid != smid)
2239                return 1;
2240        mpt2sas_base_flush_reply_queues(ioc);
2241        ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2242        mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
2243        if (mpi_reply) {
2244                memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2245                ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2246        }
2247        ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2248        complete(&ioc->tm_cmds.done);
2249        return 1;
2250}
2251
2252/**
2253 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2254 * @ioc: per adapter object
2255 * @handle: device handle
2256 *
2257 * During taskmangement request, we need to freeze the device queue.
2258 */
2259void
2260mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2261{
2262        struct MPT2SAS_DEVICE *sas_device_priv_data;
2263        struct scsi_device *sdev;
2264        u8 skip = 0;
2265
2266        shost_for_each_device(sdev, ioc->shost) {
2267                if (skip)
2268                        continue;
2269                sas_device_priv_data = sdev->hostdata;
2270                if (!sas_device_priv_data)
2271                        continue;
2272                if (sas_device_priv_data->sas_target->handle == handle) {
2273                        sas_device_priv_data->sas_target->tm_busy = 1;
2274                        skip = 1;
2275                        ioc->ignore_loginfos = 1;
2276                }
2277        }
2278}
2279
2280/**
2281 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2282 * @ioc: per adapter object
2283 * @handle: device handle
2284 *
2285 * During taskmangement request, we need to freeze the device queue.
2286 */
2287void
2288mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2289{
2290        struct MPT2SAS_DEVICE *sas_device_priv_data;
2291        struct scsi_device *sdev;
2292        u8 skip = 0;
2293
2294        shost_for_each_device(sdev, ioc->shost) {
2295                if (skip)
2296                        continue;
2297                sas_device_priv_data = sdev->hostdata;
2298                if (!sas_device_priv_data)
2299                        continue;
2300                if (sas_device_priv_data->sas_target->handle == handle) {
2301                        sas_device_priv_data->sas_target->tm_busy = 0;
2302                        skip = 1;
2303                        ioc->ignore_loginfos = 0;
2304                }
2305        }
2306}
2307
2308
2309/**
2310 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2311 * @ioc: per adapter struct
2312 * @device_handle: device handle
2313 * @channel: the channel assigned by the OS
2314 * @id: the id assigned by the OS
2315 * @lun: lun number
2316 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2317 * @smid_task: smid assigned to the task
2318 * @timeout: timeout in seconds
2319 * @serial_number: the serial_number from scmd
2320 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2321 * Context: user
2322 *
2323 * A generic API for sending task management requests to firmware.
2324 *
2325 * The callback index is set inside `ioc->tm_cb_idx`.
2326 *
2327 * Return SUCCESS or FAILED.
2328 */
2329int
2330mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2331    uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2332        unsigned long serial_number, enum mutex_type m_type)
2333{
2334        Mpi2SCSITaskManagementRequest_t *mpi_request;
2335        Mpi2SCSITaskManagementReply_t *mpi_reply;
2336        u16 smid = 0;
2337        u32 ioc_state;
2338        unsigned long timeleft;
2339        struct scsiio_tracker *scsi_lookup = NULL;
2340        int rc;
2341
2342        if (m_type == TM_MUTEX_ON)
2343                mutex_lock(&ioc->tm_cmds.mutex);
2344        if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2345                printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2346                    __func__, ioc->name);
2347                rc = FAILED;
2348                goto err_out;
2349        }
2350
2351        if (ioc->shost_recovery || ioc->remove_host ||
2352            ioc->pci_error_recovery) {
2353                printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2354                    __func__, ioc->name);
2355                rc = FAILED;
2356                goto err_out;
2357        }
2358
2359        ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2360        if (ioc_state & MPI2_DOORBELL_USED) {
2361                dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2362                    "active!\n", ioc->name));
2363                rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2364                    FORCE_BIG_HAMMER);
2365                rc = (!rc) ? SUCCESS : FAILED;
2366                goto err_out;
2367        }
2368
2369        if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2370                mpt2sas_base_fault_info(ioc, ioc_state &
2371                    MPI2_DOORBELL_DATA_MASK);
2372                rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2373                    FORCE_BIG_HAMMER);
2374                rc = (!rc) ? SUCCESS : FAILED;
2375                goto err_out;
2376        }
2377
2378        smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2379        if (!smid) {
2380                printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2381                    ioc->name, __func__);
2382                rc = FAILED;
2383                goto err_out;
2384        }
2385
2386        if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2387                scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2388
2389        dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2390            " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2391            smid_task));
2392        ioc->tm_cmds.status = MPT2_CMD_PENDING;
2393        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2394        ioc->tm_cmds.smid = smid;
2395        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2396        memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2397        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2398        mpi_request->DevHandle = cpu_to_le16(handle);
2399        mpi_request->TaskType = type;
2400        mpi_request->TaskMID = cpu_to_le16(smid_task);
2401        int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2402        mpt2sas_scsih_set_tm_flag(ioc, handle);
2403        init_completion(&ioc->tm_cmds.done);
2404        mpt2sas_base_put_smid_hi_priority(ioc, smid);
2405        timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2406        if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2407                printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2408                    ioc->name, __func__);
2409                _debug_dump_mf(mpi_request,
2410                    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2411                if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2412                        rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2413                            FORCE_BIG_HAMMER);
2414                        rc = (!rc) ? SUCCESS : FAILED;
2415                        ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2416                        mpt2sas_scsih_clear_tm_flag(ioc, handle);
2417                        goto err_out;
2418                }
2419        }
2420
2421        if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2422                mpi_reply = ioc->tm_cmds.reply;
2423                dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2424                    "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2425                    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2426                    le32_to_cpu(mpi_reply->IOCLogInfo),
2427                    le32_to_cpu(mpi_reply->TerminationCount)));
2428                if (ioc->logging_level & MPT_DEBUG_TM) {
2429                        _scsih_response_code(ioc, mpi_reply->ResponseCode);
2430                        if (mpi_reply->IOCStatus)
2431                                _debug_dump_mf(mpi_request,
2432                                    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2433                }
2434        }
2435
2436        switch (type) {
2437        case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2438                rc = SUCCESS;
2439                if (scsi_lookup->scmd == NULL)
2440                        break;
2441                rc = FAILED;
2442                break;
2443
2444        case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2445                if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2446                        rc = FAILED;
2447                else
2448                        rc = SUCCESS;
2449                break;
2450
2451        case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2452        case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2453                if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2454                        rc = FAILED;
2455                else
2456                        rc = SUCCESS;
2457                break;
2458        case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2459                rc = SUCCESS;
2460                break;
2461        default:
2462                rc = FAILED;
2463                break;
2464        }
2465
2466        mpt2sas_scsih_clear_tm_flag(ioc, handle);
2467        ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2468        if (m_type == TM_MUTEX_ON)
2469                mutex_unlock(&ioc->tm_cmds.mutex);
2470
2471        return rc;
2472
2473 err_out:
2474        if (m_type == TM_MUTEX_ON)
2475                mutex_unlock(&ioc->tm_cmds.mutex);
2476        return rc;
2477}
2478
2479/**
2480 * _scsih_tm_display_info - displays info about the device
2481 * @ioc: per adapter struct
2482 * @scmd: pointer to scsi command object
2483 *
2484 * Called by task management callback handlers.
2485 */
2486static void
2487_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2488{
2489        struct scsi_target *starget = scmd->device->sdev_target;
2490        struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2491        struct _sas_device *sas_device = NULL;
2492        unsigned long flags;
2493        char *device_str = NULL;
2494
2495        if (!priv_target)
2496                return;
2497        if (ioc->hide_ir_msg)
2498                device_str = "WarpDrive";
2499        else
2500                device_str = "volume";
2501
2502        scsi_print_command(scmd);
2503        if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2504                starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2505                    "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2506                    device_str, (unsigned long long)priv_target->sas_address);
2507        } else {
2508                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2509                sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2510                    priv_target->sas_address);
2511                if (sas_device) {
2512                        if (priv_target->flags &
2513                            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2514                                starget_printk(KERN_INFO, starget,
2515                                    "volume handle(0x%04x), "
2516                                    "volume wwid(0x%016llx)\n",
2517                                    sas_device->volume_handle,
2518                                   (unsigned long long)sas_device->volume_wwid);
2519                        }
2520                        starget_printk(KERN_INFO, starget,
2521                            "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2522                            sas_device->handle,
2523                            (unsigned long long)sas_device->sas_address,
2524                            sas_device->phy);
2525                        starget_printk(KERN_INFO, starget,
2526                            "enclosure_logical_id(0x%016llx), slot(%d)\n",
2527                           (unsigned long long)sas_device->enclosure_logical_id,
2528                            sas_device->slot);
2529                }
2530                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2531        }
2532}
2533
2534/**
2535 * _scsih_abort - eh threads main abort routine
2536 * @scmd: pointer to scsi command object
2537 *
2538 * Returns SUCCESS if command aborted else FAILED
2539 */
2540static int
2541_scsih_abort(struct scsi_cmnd *scmd)
2542{
2543        struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2544        struct MPT2SAS_DEVICE *sas_device_priv_data;
2545        u16 smid;
2546        u16 handle;
2547        int r;
2548
2549        sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2550            "scmd(%p)\n", scmd);
2551        _scsih_tm_display_info(ioc, scmd);
2552
2553        sas_device_priv_data = scmd->device->hostdata;
2554        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2555                sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2556                    "scmd(%p)\n", scmd);
2557                scmd->result = DID_NO_CONNECT << 16;
2558                scmd->scsi_done(scmd);
2559                r = SUCCESS;
2560                goto out;
2561        }
2562
2563        /* search for the command */
2564        smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2565        if (!smid) {
2566                scmd->result = DID_RESET << 16;
2567                r = SUCCESS;
2568                goto out;
2569        }
2570
2571        /* for hidden raid components and volumes this is not supported */
2572        if (sas_device_priv_data->sas_target->flags &
2573            MPT_TARGET_FLAGS_RAID_COMPONENT ||
2574            sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2575                scmd->result = DID_RESET << 16;
2576                r = FAILED;
2577                goto out;
2578        }
2579
2580        mpt2sas_halt_firmware(ioc);
2581
2582        handle = sas_device_priv_data->sas_target->handle;
2583        r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2584            scmd->device->id, scmd->device->lun,
2585            MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2586            scmd->serial_number, TM_MUTEX_ON);
2587
2588 out:
2589        sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2590            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2591        return r;
2592}
2593
2594/**
2595 * _scsih_dev_reset - eh threads main device reset routine
2596 * @scmd: pointer to scsi command object
2597 *
2598 * Returns SUCCESS if command aborted else FAILED
2599 */
2600static int
2601_scsih_dev_reset(struct scsi_cmnd *scmd)
2602{
2603        struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2604        struct MPT2SAS_DEVICE *sas_device_priv_data;
2605        struct _sas_device *sas_device;
2606        unsigned long flags;
2607        u16     handle;
2608        int r;
2609
2610        struct scsi_target *starget = scmd->device->sdev_target;
2611
2612        starget_printk(KERN_INFO, starget, "attempting device reset! "
2613            "scmd(%p)\n", scmd);
2614        _scsih_tm_display_info(ioc, scmd);
2615
2616        sas_device_priv_data = scmd->device->hostdata;
2617        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2618                starget_printk(KERN_INFO, starget, "device been deleted! "
2619                    "scmd(%p)\n", scmd);
2620                scmd->result = DID_NO_CONNECT << 16;
2621                scmd->scsi_done(scmd);
2622                r = SUCCESS;
2623                goto out;
2624        }
2625
2626        /* for hidden raid components obtain the volume_handle */
2627        handle = 0;
2628        if (sas_device_priv_data->sas_target->flags &
2629            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2630                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2631                sas_device = _scsih_sas_device_find_by_handle(ioc,
2632                   sas_device_priv_data->sas_target->handle);
2633                if (sas_device)
2634                        handle = sas_device->volume_handle;
2635                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2636        } else
2637                handle = sas_device_priv_data->sas_target->handle;
2638
2639        if (!handle) {
2640                scmd->result = DID_RESET << 16;
2641                r = FAILED;
2642                goto out;
2643        }
2644
2645        r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2646            scmd->device->id, scmd->device->lun,
2647            MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2648            TM_MUTEX_ON);
2649
2650 out:
2651        sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2652            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2653        return r;
2654}
2655
2656/**
2657 * _scsih_target_reset - eh threads main target reset routine
2658 * @scmd: pointer to scsi command object
2659 *
2660 * Returns SUCCESS if command aborted else FAILED
2661 */
2662static int
2663_scsih_target_reset(struct scsi_cmnd *scmd)
2664{
2665        struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2666        struct MPT2SAS_DEVICE *sas_device_priv_data;
2667        struct _sas_device *sas_device;
2668        unsigned long flags;
2669        u16     handle;
2670        int r;
2671        struct scsi_target *starget = scmd->device->sdev_target;
2672
2673        starget_printk(KERN_INFO, starget, "attempting target reset! "
2674            "scmd(%p)\n", scmd);
2675        _scsih_tm_display_info(ioc, scmd);
2676
2677        sas_device_priv_data = scmd->device->hostdata;
2678        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2679                starget_printk(KERN_INFO, starget, "target been deleted! "
2680                    "scmd(%p)\n", scmd);
2681                scmd->result = DID_NO_CONNECT << 16;
2682                scmd->scsi_done(scmd);
2683                r = SUCCESS;
2684                goto out;
2685        }
2686
2687        /* for hidden raid components obtain the volume_handle */
2688        handle = 0;
2689        if (sas_device_priv_data->sas_target->flags &
2690            MPT_TARGET_FLAGS_RAID_COMPONENT) {
2691                spin_lock_irqsave(&ioc->sas_device_lock, flags);
2692                sas_device = _scsih_sas_device_find_by_handle(ioc,
2693                   sas_device_priv_data->sas_target->handle);
2694                if (sas_device)
2695                        handle = sas_device->volume_handle;
2696                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2697        } else
2698                handle = sas_device_priv_data->sas_target->handle;
2699
2700        if (!handle) {
2701                scmd->result = DID_RESET << 16;
2702                r = FAILED;
2703                goto out;
2704        }
2705
2706        r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2707            scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2708            30, 0, TM_MUTEX_ON);
2709
2710 out:
2711        starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2712            ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2713        return r;
2714}
2715
2716/**
2717 * _scsih_host_reset - eh threads main host reset routine
2718 * @scmd: pointer to scsi command object
2719 *
2720 * Returns SUCCESS if command aborted else FAILED
2721 */
2722static int
2723_scsih_host_reset(struct scsi_cmnd *scmd)
2724{
2725        struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2726        int r, retval;
2727
2728        printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2729            ioc->name, scmd);
2730        scsi_print_command(scmd);
2731
2732        retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2733            FORCE_BIG_HAMMER);
2734        r = (retval < 0) ? FAILED : SUCCESS;
2735        printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2736            ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2737
2738        return r;
2739}
2740
2741/**
2742 * _scsih_fw_event_add - insert and queue up fw_event
2743 * @ioc: per adapter object
2744 * @fw_event: object describing the event
2745 * Context: This function will acquire ioc->fw_event_lock.
2746 *
2747 * This adds the firmware event object into link list, then queues it up to
2748 * be processed from user context.
2749 *
2750 * Return nothing.
2751 */
2752static void
2753_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2754{
2755        unsigned long flags;
2756
2757        if (ioc->firmware_event_thread == NULL)
2758                return;
2759
2760        spin_lock_irqsave(&ioc->fw_event_lock, flags);
2761        list_add_tail(&fw_event->list, &ioc->fw_event_list);
2762        INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2763        queue_delayed_work(ioc->firmware_event_thread,
2764            &fw_event->delayed_work, 0);
2765        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2766}
2767
2768/**
2769 * _scsih_fw_event_free - delete fw_event
2770 * @ioc: per adapter object
2771 * @fw_event: object describing the event
2772 * Context: This function will acquire ioc->fw_event_lock.
2773 *
2774 * This removes firmware event object from link list, frees associated memory.
2775 *
2776 * Return nothing.
2777 */
2778static void
2779_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2780    *fw_event)
2781{
2782        unsigned long flags;
2783
2784        spin_lock_irqsave(&ioc->fw_event_lock, flags);
2785        list_del(&fw_event->list);
2786        kfree(fw_event->event_data);
2787        kfree(fw_event);
2788        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2789}
2790
2791
2792/**
2793 * _scsih_error_recovery_delete_devices - remove devices not responding
2794 * @ioc: per adapter object
2795 *
2796 * Return nothing.
2797 */
2798static void
2799_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
2800{
2801        struct fw_event_work *fw_event;
2802
2803        if (ioc->is_driver_loading)
2804                return;
2805
2806        fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2807        if (!fw_event)
2808                return;
2809
2810        fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2811        fw_event->ioc = ioc;
2812        _scsih_fw_event_add(ioc, fw_event);
2813}
2814
2815/**
2816 * mpt2sas_port_enable_complete - port enable completed (fake event)
2817 * @ioc: per adapter object
2818 *
2819 * Return nothing.
2820 */
2821void
2822mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2823{
2824        struct fw_event_work *fw_event;
2825
2826        fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2827        if (!fw_event)
2828                return;
2829        fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
2830        fw_event->ioc = ioc;
2831        _scsih_fw_event_add(ioc, fw_event);
2832}
2833
2834/**
2835 * _scsih_fw_event_cleanup_queue - cleanup event queue
2836 * @ioc: per adapter object
2837 *
2838 * Walk the firmware event queue, either killing timers, or waiting
2839 * for outstanding events to complete
2840 *
2841 * Return nothing.
2842 */
2843static void
2844_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2845{
2846        struct fw_event_work *fw_event, *next;
2847
2848        if (list_empty(&ioc->fw_event_list) ||
2849             !ioc->firmware_event_thread || in_interrupt())
2850                return;
2851
2852        list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2853                if (cancel_delayed_work(&fw_event->delayed_work)) {
2854                        _scsih_fw_event_free(ioc, fw_event);
2855                        continue;
2856                }
2857                fw_event->cancel_pending_work = 1;
2858        }
2859}
2860
2861/**
2862 * _scsih_ublock_io_all_device - unblock every device
2863 * @ioc: per adapter object
2864 *
2865 * change the device state from block to running
2866 */
2867static void
2868_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2869{
2870        struct MPT2SAS_DEVICE *sas_device_priv_data;
2871        struct scsi_device *sdev;
2872
2873        shost_for_each_device(sdev, ioc->shost) {
2874                sas_device_priv_data = sdev->hostdata;
2875                if (!sas_device_priv_data)
2876                        continue;
2877                if (!sas_device_priv_data->block)
2878                        continue;
2879                sas_device_priv_data->block = 0;
2880                dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2881                    "handle(0x%04x)\n",
2882                    sas_device_priv_data->sas_target->handle));
2883                scsi_internal_device_unblock(sdev);
2884        }
2885}
2886/**
2887 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2888 * @ioc: per adapter object
2889 * @handle: device handle
2890 *
2891 * During device pull we need to appropiately set the sdev state.
2892 */
2893static void
2894_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2895{
2896        struct MPT2SAS_DEVICE *sas_device_priv_data;
2897        struct scsi_device *sdev;
2898
2899        shost_for_each_device(sdev, ioc->shost) {
2900                sas_device_priv_data = sdev->hostdata;
2901                if (!sas_device_priv_data)
2902                        continue;
2903                if (!sas_device_priv_data->block)
2904                        continue;
2905                if (sas_device_priv_data->sas_target->handle == handle) {
2906                        dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2907                            MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2908                            "handle(0x%04x)\n", ioc->name, handle));
2909                        sas_device_priv_data->block = 0;
2910                        scsi_internal_device_unblock(sdev);
2911                }
2912        }
2913}
2914
2915/**
2916 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2917 * @ioc: per adapter object
2918 * @handle: device handle
2919 *
2920 * During device pull we need to appropiately set the sdev state.
2921 */
2922static void
2923_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2924{
2925        struct MPT2SAS_DEVICE *sas_device_priv_data;
2926        struct scsi_device *sdev;
2927
2928        shost_for_each_device(sdev, ioc->shost) {
2929                sas_device_priv_data = sdev->hostdata;
2930                if (!sas_device_priv_data)
2931                        continue;
2932                if (sas_device_priv_data->block)
2933                        continue;
2934                sas_device_priv_data->block = 1;
2935                dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2936                    "handle(0x%04x)\n",
2937                    sas_device_priv_data->sas_target->handle));
2938                scsi_internal_device_block(sdev);
2939        }
2940}
2941
2942
2943/**
2944 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2945 * @ioc: per adapter object
2946 * @handle: device handle
2947 *
2948 * During device pull we need to appropiately set the sdev state.
2949 */
2950static void
2951_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2952{
2953        struct MPT2SAS_DEVICE *sas_device_priv_data;
2954        struct scsi_device *sdev;
2955
2956        shost_for_each_device(sdev, ioc->shost) {
2957                sas_device_priv_data = sdev->hostdata;
2958                if (!sas_device_priv_data)
2959                        continue;
2960                if (sas_device_priv_data->block)
2961                        continue;
2962                if (sas_device_priv_data->sas_target->handle == handle) {
2963                        dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2964                            MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2965                            "handle(0x%04x)\n", ioc->name, handle));
2966                        sas_device_priv_data->block = 1;
2967                        scsi_internal_device_block(sdev);
2968                }
2969        }
2970}
2971
2972/**
2973 * _scsih_block_io_to_children_attached_to_ex
2974 * @ioc: per adapter object
2975 * @sas_expander: the sas_device object
2976 *
2977 * This routine set sdev state to SDEV_BLOCK for all devices
2978 * attached to this expander. This function called when expander is
2979 * pulled.
2980 */
2981static void
2982_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2983    struct _sas_node *sas_expander)
2984{
2985        struct _sas_port *mpt2sas_port;
2986        struct _sas_device *sas_device;
2987        struct _sas_node *expander_sibling;
2988        unsigned long flags;
2989
2990        if (!sas_expander)
2991                return;
2992
2993        list_for_each_entry(mpt2sas_port,
2994           &sas_expander->sas_port_list, port_list) {
2995                if (mpt2sas_port->remote_identify.device_type ==
2996                    SAS_END_DEVICE) {
2997                        spin_lock_irqsave(&ioc->sas_device_lock, flags);
2998                        sas_device =
2999                            mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3000                           mpt2sas_port->remote_identify.sas_address);
3001                        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3002                        if (!sas_device)
3003                                continue;
3004                        _scsih_block_io_device(ioc, sas_device->handle);
3005                }
3006        }
3007
3008        list_for_each_entry(mpt2sas_port,
3009           &sas_expander->sas_port_list, port_list) {
3010
3011                if (mpt2sas_port->remote_identify.device_type ==
3012                    SAS_EDGE_EXPANDER_DEVICE ||
3013                    mpt2sas_port->remote_identify.device_type ==
3014                    SAS_FANOUT_EXPANDER_DEVICE) {
3015
3016                        spin_lock_irqsave(&ioc->sas_node_lock, flags);
3017                        expander_sibling =
3018                            mpt2sas_scsih_expander_find_by_sas_address(
3019                            ioc, mpt2sas_port->remote_identify.sas_address);
3020                        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3021                        _scsih_block_io_to_children_attached_to_ex(ioc,
3022                            expander_sibling);
3023                }
3024        }
3025}
3026
3027/**
3028 * _scsih_block_io_to_children_attached_directly
3029 * @ioc: per adapter object
3030 * @event_data: topology change event data
3031 *
3032 * This routine set sdev state to SDEV_BLOCK for all devices
3033 * direct attached during device pull.
3034 */
3035static void
3036_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3037    Mpi2EventDataSasTopologyChangeList_t *event_data)
3038{
3039        int i;
3040        u16 handle;
3041        u16 reason_code;
3042        u8 phy_number;
3043
3044        for (i = 0; i < event_data->NumEntries; i++) {
3045                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3046                if (!handle)
3047                        continue;
3048                phy_number = event_data->StartPhyNum + i;
3049                reason_code = event_data->PHY[i].PhyStatus &
3050                    MPI2_EVENT_SAS_TOPO_RC_MASK;
3051                if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3052                        _scsih_block_io_device(ioc, handle);
3053        }
3054}
3055
3056/**
3057 * _scsih_tm_tr_send - send task management request
3058 * @ioc: per adapter object
3059 * @handle: device handle
3060 * Context: interrupt time.
3061 *
3062 * This code is to initiate the device removal handshake protocol
3063 * with controller firmware.  This function will issue target reset
3064 * using high priority request queue.  It will send a sas iounit
3065 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3066 *
3067 * This is designed to send muliple task management request at the same
3068 * time to the fifo. If the fifo is full, we will append the request,
3069 * and process it in a future completion.
3070 */
3071static void
3072_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3073{
3074        Mpi2SCSITaskManagementRequest_t *mpi_request;
3075        u16 smid;
3076        struct _sas_device *sas_device;
3077        struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3078        u64 sas_address = 0;
3079        unsigned long flags;
3080        struct _tr_list *delayed_tr;
3081        u32 ioc_state;
3082
3083        if (ioc->remove_host) {
3084                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3085                    "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3086                return;
3087        } else if (ioc->pci_error_recovery) {
3088                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3089                    "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3090                    handle));
3091                return;
3092        }
3093        ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3094        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3095                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3096                   "operational: handle(0x%04x)\n", __func__, ioc->name,
3097                   handle));
3098                return;
3099        }
3100
3101        /* if PD, then return */
3102        if (test_bit(handle, ioc->pd_handles))
3103                return;
3104
3105        spin_lock_irqsave(&ioc->sas_device_lock, flags);
3106        sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3107        if (sas_device && sas_device->starget &&
3108             sas_device->starget->hostdata) {
3109                sas_target_priv_data = sas_device->starget->hostdata;
3110                sas_target_priv_data->deleted = 1;
3111                sas_address = sas_device->sas_address;
3112        }
3113        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3114
3115        if (sas_target_priv_data) {
3116                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3117                "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3118                        (unsigned long long)sas_address));
3119                _scsih_ublock_io_device(ioc, handle);
3120                sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
3121        }
3122
3123        smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3124        if (!smid) {
3125                delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3126                if (!delayed_tr)
3127                        return;
3128                INIT_LIST_HEAD(&delayed_tr->list);
3129                delayed_tr->handle = handle;
3130                list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3131                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3132                    "DELAYED:tr:handle(0x%04x), (open)\n",
3133                    ioc->name, handle));
3134                return;
3135        }
3136
3137        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3138            "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3139            ioc->tm_tr_cb_idx));
3140        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3141        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3142        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3143        mpi_request->DevHandle = cpu_to_le16(handle);
3144        mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3145        mpt2sas_base_put_smid_hi_priority(ioc, smid);
3146}
3147
3148
3149
3150/**
3151 * _scsih_sas_control_complete - completion routine
3152 * @ioc: per adapter object
3153 * @smid: system request message index
3154 * @msix_index: MSIX table index supplied by the OS
3155 * @reply: reply message frame(lower 32bit addr)
3156 * Context: interrupt time.
3157 *
3158 * This is the sas iounit control completion routine.
3159 * This code is part of the code to initiate the device removal
3160 * handshake protocol with controller firmware.
3161 *
3162 * Return 1 meaning mf should be freed from _base_interrupt
3163 *        0 means the mf is freed from this function.
3164 */
3165static u8
3166_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3167    u8 msix_index, u32 reply)
3168{
3169#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3170        Mpi2SasIoUnitControlReply_t *mpi_reply =
3171            mpt2sas_base_get_reply_virt_addr(ioc, reply);
3172#endif
3173        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3174            "sc_complete:handle(0x%04x), (open) "
3175            "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3176            ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3177            le16_to_cpu(mpi_reply->IOCStatus),
3178            le32_to_cpu(mpi_reply->IOCLogInfo)));
3179        return 1;
3180}
3181
3182/**
3183 * _scsih_tm_tr_volume_send - send target reset request for volumes
3184 * @ioc: per adapter object
3185 * @handle: device handle
3186 * Context: interrupt time.
3187 *
3188 * This is designed to send muliple task management request at the same
3189 * time to the fifo. If the fifo is full, we will append the request,
3190 * and process it in a future completion.
3191 */
3192static void
3193_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3194{
3195        Mpi2SCSITaskManagementRequest_t *mpi_request;
3196        u16 smid;
3197        struct _tr_list *delayed_tr;
3198
3199        if (ioc->shost_recovery || ioc->remove_host ||
3200            ioc->pci_error_recovery) {
3201                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3202                   "progress!\n", __func__, ioc->name));
3203                return;
3204        }
3205
3206        smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3207        if (!smid) {
3208                delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3209                if (!delayed_tr)
3210                        return;
3211                INIT_LIST_HEAD(&delayed_tr->list);
3212                delayed_tr->handle = handle;
3213                list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3214                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3215                    "DELAYED:tr:handle(0x%04x), (open)\n",
3216                    ioc->name, handle));
3217                return;
3218        }
3219
3220        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3221            "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3222            ioc->tm_tr_volume_cb_idx));
3223        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3224        memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3225        mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3226        mpi_request->DevHandle = cpu_to_le16(handle);
3227        mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3228        mpt2sas_base_put_smid_hi_priority(ioc, smid);
3229}
3230
3231/**
3232 * _scsih_tm_volume_tr_complete - target reset completion
3233 * @ioc: per adapter object
3234 * @smid: system request message index
3235 * @msix_index: MSIX table index supplied by the OS
3236 * @reply: reply message frame(lower 32bit addr)
3237 * Context: interrupt time.
3238 *
3239 * Return 1 meaning mf should be freed from _base_interrupt
3240 *        0 means the mf is freed from this function.
3241 */
3242static u8
3243_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3244    u8 msix_index, u32 reply)
3245{
3246        u16 handle;
3247        Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3248        Mpi2SCSITaskManagementReply_t *mpi_reply =
3249            mpt2sas_base_get_reply_virt_addr(ioc, reply);
3250
3251        if (ioc->shost_recovery || ioc->remove_host ||
3252            ioc->pci_error_recovery) {
3253                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3254                   "progress!\n", __func__, ioc->name));
3255                return 1;
3256        }
3257
3258        mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3259        handle = le16_to_cpu(mpi_request_tm->DevHandle);
3260        if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3261                dewtprintk(ioc, printk("spurious interrupt: "
3262                    "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3263                    le16_to_cpu(mpi_reply->DevHandle), smid));
3264                return 0;
3265        }
3266
3267        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3268            "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3269            "loginfo(0x%08x), completed(%d)\n", ioc->name,
3270            handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3271            le32_to_cpu(mpi_reply->IOCLogInfo),
3272            le32_to_cpu(mpi_reply->TerminationCount)));
3273
3274        return _scsih_check_for_pending_tm(ioc, smid);
3275}
3276
3277/**
3278 * _scsih_tm_tr_complete -
3279 * @ioc: per adapter object
3280 * @smid: system request message index
3281 * @msix_index: MSIX table index supplied by the OS
3282 * @reply: reply message frame(lower 32bit addr)
3283 * Context: interrupt time.
3284 *
3285 * This is the target reset completion routine.
3286 * This code is part of the code to initiate the device removal
3287 * handshake protocol with controller firmware.
3288 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3289 *
3290 * Return 1 meaning mf should be freed from _base_interrupt
3291 *        0 means the mf is freed from this function.
3292 */
3293static u8
3294_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3295    u32 reply)
3296{
3297        u16 handle;
3298        Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3299        Mpi2SCSITaskManagementReply_t *mpi_reply =
3300            mpt2sas_base_get_reply_virt_addr(ioc, reply);
3301        Mpi2SasIoUnitControlRequest_t *mpi_request;
3302        u16 smid_sas_ctrl;
3303        u32 ioc_state;
3304
3305        if (ioc->remove_host) {
3306                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3307                   "removed\n", __func__, ioc->name));
3308                return 1;
3309        } else if (ioc->pci_error_recovery) {
3310                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3311                    "error recovery\n", __func__, ioc->name));
3312                return 1;
3313        }
3314        ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3315        if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3316                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3317                    "operational\n", __func__, ioc->name));
3318                return 1;
3319        }
3320
3321        mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3322        handle = le16_to_cpu(mpi_request_tm->DevHandle);
3323        if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3324                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3325                    "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3326                    le16_to_cpu(mpi_reply->DevHandle), smid));
3327                return 0;
3328        }
3329
3330        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3331            "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3332            "loginfo(0x%08x), completed(%d)\n", ioc->name,
3333            handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3334            le32_to_cpu(mpi_reply->IOCLogInfo),
3335            le32_to_cpu(mpi_reply->TerminationCount)));
3336
3337        smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3338        if (!smid_sas_ctrl) {
3339                printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3340                    ioc->name, __func__);
3341                return 1;
3342        }
3343
3344        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3345            "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3346            ioc->tm_sas_control_cb_idx));
3347        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3348        memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3349        mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3350        mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3351        mpi_request->DevHandle = mpi_request_tm->DevHandle;
3352        mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
3353
3354        return _scsih_check_for_pending_tm(ioc, smid);
3355}
3356
3357/**
3358 * _scsih_check_for_pending_tm - check for pending task management
3359 * @ioc: per adapter object
3360 * @smid: system request message index
3361 *
3362 * This will check delayed target reset list, and feed the
3363 * next reqeust.
3364 *
3365 * Return 1 meaning mf should be freed from _base_interrupt
3366 *        0 means the mf is freed from this function.
3367 */
3368static u8
3369_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3370{
3371        struct _tr_list *delayed_tr;
3372
3373        if (!list_empty(&ioc->delayed_tr_volume_list)) {
3374                delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3375                    struct _tr_list, list);
3376                mpt2sas_base_free_smid(ioc, smid);
3377                _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3378                list_del(&delayed_tr->list);
3379                kfree(delayed_tr);
3380                return 0;
3381        }
3382
3383        if (!list_empty(&ioc->delayed_tr_list)) {
3384                delayed_tr = list_entry(ioc->delayed_tr_list.next,
3385                    struct _tr_list, list);
3386                mpt2sas_base_free_smid(ioc, smid);
3387                _scsih_tm_tr_send(ioc, delayed_tr->handle);
3388                list_del(&delayed_tr->list);
3389                kfree(delayed_tr);
3390                return 0;
3391        }
3392
3393        return 1;
3394}
3395
3396/**
3397 * _scsih_check_topo_delete_events - sanity check on topo events
3398 * @ioc: per adapter object
3399 * @event_data: the event data payload
3400 *
3401 * This routine added to better handle cable breaker.
3402 *
3403 * This handles the case where driver receives multiple expander
3404 * add and delete events in a single shot.  When there is a delete event
3405 * the routine will void any pending add events waiting in the event queue.
3406 *
3407 * Return nothing.
3408 */
3409static void
3410_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3411    Mpi2EventDataSasTopologyChangeList_t *event_data)
3412{
3413        struct fw_event_work *fw_event;
3414        Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3415        u16 expander_handle;
3416        struct _sas_node *sas_expander;
3417        unsigned long flags;
3418        int i, reason_code;
3419        u16 handle;
3420
3421        for (i = 0 ; i < event_data->NumEntries; i++) {
3422                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3423                if (!handle)
3424                        continue;
3425                reason_code = event_data->PHY[i].PhyStatus &
3426                    MPI2_EVENT_SAS_TOPO_RC_MASK;
3427                if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3428                        _scsih_tm_tr_send(ioc, handle);
3429        }
3430
3431        expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3432        if (expander_handle < ioc->sas_hba.num_phys) {
3433                _scsih_block_io_to_children_attached_directly(ioc, event_data);
3434                return;
3435        }
3436
3437        if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3438         || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3439                spin_lock_irqsave(&ioc->sas_node_lock, flags);
3440                sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3441                    expander_handle);
3442                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3443                _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3444        } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3445                _scsih_block_io_to_children_attached_directly(ioc, event_data);
3446
3447        if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3448                return;
3449
3450        /* mark ignore flag for pending events */
3451        spin_lock_irqsave(&ioc->fw_event_lock, flags);
3452        list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3453                if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3454                    fw_event->ignore)
3455                        continue;
3456                local_event_data = fw_event->event_data;
3457                if (local_event_data->ExpStatus ==
3458                    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3459                    local_event_data->ExpStatus ==
3460                    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3461                        if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3462                            expander_handle) {
3463                                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3464                                    "setting ignoring flag\n", ioc->name));
3465                                fw_event->ignore = 1;
3466                        }
3467                }
3468        }
3469        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3470}
3471
3472/**
3473 * _scsih_set_volume_delete_flag - setting volume delete flag
3474 * @ioc: per adapter object
3475 * @handle: device handle
3476 *
3477 * This
3478 * Return nothing.
3479 */
3480static void
3481_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3482{
3483        struct _raid_device *raid_device;
3484        struct MPT2SAS_TARGET *sas_target_priv_data;
3485        unsigned long flags;
3486
3487        spin_lock_irqsave(&ioc->raid_device_lock, flags);
3488        raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3489        if (raid_device && raid_device->starget &&
3490            raid_device->starget->hostdata) {
3491                sas_target_priv_data =
3492                    raid_device->starget->hostdata;
3493                sas_target_priv_data->deleted = 1;
3494                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3495                    "setting delete flag: handle(0x%04x), "
3496                    "wwid(0x%016llx)\n", ioc->name, handle,
3497                    (unsigned long long) raid_device->wwid));
3498        }
3499        spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3500}
3501
3502/**
3503 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3504 * @handle: input handle
3505 * @a: handle for volume a
3506 * @b: handle for volume b
3507 *
3508 * IR firmware only supports two raid volumes.  The purpose of this
3509 * routine is to set the volume handle in either a or b. When the given
3510 * input handle is non-zero, or when a and b have not been set before.
3511 */
3512static void
3513_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3514{
3515        if (!handle || handle == *a || handle == *b)
3516                return;
3517        if (!*a)
3518                *a = handle;
3519        else if (!*b)
3520                *b = handle;
3521}
3522
3523/**
3524 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3525 * @ioc: per adapter object
3526 * @event_data: the event data payload
3527 * Context: interrupt time.
3528 *
3529 * This routine will send target reset to volume, followed by target
3530 * resets to the PDs. This is called when a PD has been removed, or
3531 * volume has been deleted or removed. When the target reset is sent
3532 * to volume, the PD target resets need to be queued to start upon
3533 * completion of the volume target reset.
3534 *
3535 * Return nothing.
3536 */
3537static void
3538_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3539    Mpi2EventDataIrConfigChangeList_t *event_data)
3540{
3541        Mpi2EventIrConfigElement_t *element;
3542        int i;
3543        u16 handle, volume_handle, a, b;
3544        struct _tr_list *delayed_tr;
3545
3546        a = 0;
3547        b = 0;
3548
3549        if (ioc->is_warpdrive)
3550                return;
3551
3552        /* Volume Resets for Deleted or Removed */
3553        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3554        for (i = 0; i < event_data->NumElements; i++, element++) {
3555                if (element->ReasonCode ==
3556                    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3557                    element->ReasonCode ==
3558                    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3559                        volume_handle = le16_to_cpu(element->VolDevHandle);
3560                        _scsih_set_volume_delete_flag(ioc, volume_handle);
3561                        _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3562                }
3563        }
3564
3565        /* Volume Resets for UNHIDE events */
3566        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3567        for (i = 0; i < event_data->NumElements; i++, element++) {
3568                if (le32_to_cpu(event_data->Flags) &
3569                    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3570                        continue;
3571                if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3572                        volume_handle = le16_to_cpu(element->VolDevHandle);
3573                        _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3574                }
3575        }
3576
3577        if (a)
3578                _scsih_tm_tr_volume_send(ioc, a);
3579        if (b)
3580                _scsih_tm_tr_volume_send(ioc, b);
3581
3582        /* PD target resets */
3583        element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3584        for (i = 0; i < event_data->NumElements; i++, element++) {
3585                if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3586                        continue;
3587                handle = le16_to_cpu(element->PhysDiskDevHandle);
3588                volume_handle = le16_to_cpu(element->VolDevHandle);
3589                clear_bit(handle, ioc->pd_handles);
3590                if (!volume_handle)
3591                        _scsih_tm_tr_send(ioc, handle);
3592                else if (volume_handle == a || volume_handle == b) {
3593                        delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3594                        BUG_ON(!delayed_tr);
3595                        INIT_LIST_HEAD(&delayed_tr->list);
3596                        delayed_tr->handle = handle;
3597                        list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3598                        dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3599                            "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3600                            handle));
3601                } else
3602                        _scsih_tm_tr_send(ioc, handle);
3603        }
3604}
3605
3606
3607/**
3608 * _scsih_check_volume_delete_events - set delete flag for volumes
3609 * @ioc: per adapter object
3610 * @event_data: the event data payload
3611 * Context: interrupt time.
3612 *
3613 * This will handle the case when the cable connected to entire volume is
3614 * pulled. We will take care of setting the deleted flag so normal IO will
3615 * not be sent.
3616 *
3617 * Return nothing.
3618 */
3619static void
3620_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3621    Mpi2EventDataIrVolume_t *event_data)
3622{
3623        u32 state;
3624
3625        if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3626                return;
3627        state = le32_to_cpu(event_data->NewValue);
3628        if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3629            MPI2_RAID_VOL_STATE_FAILED)
3630                _scsih_set_volume_delete_flag(ioc,
3631                    le16_to_cpu(event_data->VolDevHandle));
3632}
3633
3634/**
3635 * _scsih_flush_running_cmds - completing outstanding commands.
3636 * @ioc: per adapter object
3637 *
3638 * The flushing out of all pending scmd commands following host reset,
3639 * where all IO is dropped to the floor.
3640 *
3641 * Return nothing.
3642 */
3643static void
3644_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3645{
3646        struct scsi_cmnd *scmd;
3647        u16 smid;
3648        u16 count = 0;
3649
3650        for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3651                scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3652                if (!scmd)
3653                        continue;
3654                count++;
3655                mpt2sas_base_free_smid(ioc, smid);
3656                scsi_dma_unmap(scmd);
3657                if (ioc->pci_error_recovery)
3658                        scmd->result = DID_NO_CONNECT << 16;
3659                else
3660                        scmd->result = DID_RESET << 16;
3661                scmd->scsi_done(scmd);
3662        }
3663        dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3664            ioc->name, count));
3665}
3666
3667/**
3668 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3669 * @scmd: pointer to scsi command object
3670 * @mpi_request: pointer to the SCSI_IO reqest message frame
3671 *
3672 * Supporting protection 1 and 3.
3673 *
3674 * Returns nothing
3675 */
3676static void
3677_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3678{
3679        u16 eedp_flags;
3680        unsigned char prot_op = scsi_get_prot_op(scmd);
3681        unsigned char prot_type = scsi_get_prot_type(scmd);
3682
3683        if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3684                return;
3685
3686        if (prot_op ==  SCSI_PROT_READ_STRIP)
3687                eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3688        else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3689                eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3690        else
3691                return;
3692
3693        switch (prot_type) {
3694        case SCSI_PROT_DIF_TYPE1:
3695        case SCSI_PROT_DIF_TYPE2:
3696
3697                /*
3698                * enable ref/guard checking
3699                * auto increment ref tag
3700                */
3701                eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3702                    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3703                    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3704                mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3705                    cpu_to_be32(scsi_get_lba(scmd));
3706                break;
3707
3708        case SCSI_PROT_DIF_TYPE3:
3709
3710                /*
3711                * enable guard checking
3712                */
3713                eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3714                break;
3715        }
3716        mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3717        mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3718}
3719
3720/**
3721 * _scsih_eedp_error_handling - return sense code for EEDP errors
3722 * @scmd: pointer to scsi command object
3723 * @ioc_status: ioc status
3724 *
3725 * Returns nothing
3726 */
3727static void
3728_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3729{
3730        u8 ascq;
3731        u8 sk;
3732        u8 host_byte;
3733
3734        switch (ioc_status) {
3735        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3736                ascq = 0x01;
3737                break;
3738        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3739                ascq = 0x02;
3740                break;
3741        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3742                ascq = 0x03;
3743                break;
3744        default:
3745                ascq = 0x00;
3746                break;
3747        }
3748
3749        if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3750                sk = ILLEGAL_REQUEST;
3751                host_byte = DID_ABORT;
3752        } else {
3753                sk = ABORTED_COMMAND;
3754                host_byte = DID_OK;
3755        }
3756
3757        scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3758        scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3759            SAM_STAT_CHECK_CONDITION;
3760}
3761
3762/**
3763 * _scsih_scsi_direct_io_get - returns direct io flag
3764 * @ioc: per adapter object
3765 * @smid: system request message index
3766 *
3767 * Returns the smid stored scmd pointer.
3768 */
3769static inline u8
3770_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3771{
3772        return ioc->scsi_lookup[smid - 1].direct_io;
3773}
3774
3775/**
3776 * _scsih_scsi_direct_io_set - sets direct io flag
3777 * @ioc: per adapter object
3778 * @smid: system request message index
3779 * @direct_io: Zero or non-zero value to set in the direct_io flag
3780 *
3781 * Returns Nothing.
3782 */
3783static inline void
3784_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3785{
3786        ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3787}
3788
3789
3790/**
3791 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3792 * @ioc: per adapter object
3793 * @scmd: pointer to scsi command object
3794 * @raid_device: pointer to raid device data structure
3795 * @mpi_request: pointer to the SCSI_IO reqest message frame
3796 * @smid: system request message index
3797 *
3798 * Returns nothing
3799 */
3800static void
3801_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3802        struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3803        u16 smid)
3804{
3805        u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3806        u32 stripe_sz, stripe_exp;
3807        u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3808        u8 cdb0 = scmd->cmnd[0];
3809
3810        /*
3811         * Try Direct I/O to RAID memeber disks
3812         */
3813        if (cdb0 == READ_16 || cdb0 == READ_10 ||
3814            cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3815                cdb_ptr = mpi_request->CDB.CDB32;
3816
3817                if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3818                        | cdb_ptr[5])) {
3819                        io_size = scsi_bufflen(scmd) >> 9;
3820                        /* get virtual lba */
3821                        lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3822                            &cdb_ptr[6];
3823                        tmp_ptr = (u8 *)&v_lba + 3;
3824                        *tmp_ptr-- = *lba_ptr1++;
3825                        *tmp_ptr-- = *lba_ptr1++;
3826                        *tmp_ptr-- = *lba_ptr1++;
3827                        *tmp_ptr = *lba_ptr1;
3828
3829                        if (((u64)v_lba + (u64)io_size - 1) <=
3830                            (u32)raid_device->max_lba) {
3831                                stripe_sz = raid_device->stripe_sz;
3832                                stripe_exp = raid_device->stripe_exponent;
3833                                stripe_off = v_lba & (stripe_sz - 1);
3834
3835                                /* Check whether IO falls within a stripe */
3836                                if ((stripe_off + io_size) <= stripe_sz) {
3837                                        num_pds = raid_device->num_pds;
3838                                        p_lba = v_lba >> stripe_exp;
3839                                        stripe_unit = p_lba / num_pds;
3840                                        column = p_lba % num_pds;
3841                                        p_lba = (stripe_unit << stripe_exp) +
3842                                            stripe_off;
3843                                        mpi_request->DevHandle =
3844                                                cpu_to_le16(raid_device->
3845                                                    pd_handle[column]);
3846                                        tmp_ptr = (u8 *)&p_lba + 3;
3847                                        *lba_ptr2++ = *tmp_ptr--;
3848                                        *lba_ptr2++ = *tmp_ptr--;
3849                                        *lba_ptr2++ = *tmp_ptr--;
3850                                        *lba_ptr2 = *tmp_ptr;
3851                                        /*
3852                                        * WD: To indicate this I/O is directI/O
3853                                        */
3854                                        _scsih_scsi_direct_io_set(ioc, smid, 1);
3855                                }
3856                        }
3857                }
3858        }
3859}
3860
3861/**
3862 * _scsih_qcmd - main scsi request entry point
3863 * @scmd: pointer to scsi command object
3864 * @done: function pointer to be invoked on completion
3865 *
3866 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3867 *
3868 * Returns 0 on success.  If there's a failure, return either:
3869 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3870 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3871 */
3872static int
3873_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
3874{
3875        struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3876        struct MPT2SAS_DEVICE *sas_device_priv_data;
3877        struct MPT2SAS_TARGET *sas_target_priv_data;
3878        struct _raid_device *raid_device;
3879        Mpi2SCSIIORequest_t *mpi_request;
3880        u32 mpi_control;
3881        u16 smid;
3882
3883        scmd->scsi_done = done;
3884        sas_device_priv_data = scmd->device->hostdata;
3885        if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3886                scmd->result = DID_NO_CONNECT << 16;
3887                scmd->scsi_done(scmd);
3888                return 0;
3889        }
3890
3891        if (ioc->pci_error_recovery || ioc->remove_host) {
3892                scmd->result = DID_NO_CONNECT << 16;
3893                scmd->scsi_done(scmd);
3894                return 0;
3895        }
3896
3897        sas_target_priv_data = sas_device_priv_data->sas_target;
3898        /* invalid device handle */
3899        if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3900                scmd->result = DID_NO_CONNECT << 16;
3901                scmd->scsi_done(scmd);
3902                return 0;
3903        }
3904
3905        /* host recovery or link resets sent via IOCTLs */
3906        if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3907                return SCSI_MLQUEUE_HOST_BUSY;
3908        /* device busy with task management */
3909        else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3910                return SCSI_MLQUEUE_DEVICE_BUSY;
3911        /* device has been deleted */
3912        else if (sas_target_priv_data->deleted) {
3913                scmd->result = DID_NO_CONNECT << 16;
3914                scmd->scsi_done(scmd);
3915                return 0;
3916        }
3917
3918        if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3919                mpi_control = MPI2_SCSIIO_CONTROL_READ;
3920        else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3921                mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3922        else
3923                mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3924
3925        /* set tags */
3926        if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3927                if (scmd->device->tagged_supported) {
3928                        if (scmd->device->ordered_tags)
3929                                mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3930                        else
3931                                mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3932                } else
3933/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3934/*                      mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3935 */
3936                        mpi_control |= (0x500);
3937
3938        } else
3939                mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3940        /* Make sure Device is not raid volume.
3941         * We do not expose raid functionality to upper layer for warpdrive.
3942         */
3943        if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
3944            sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3945                mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3946
3947        smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3948        if (!smid) {
3949                printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3950                    ioc->name, __func__);
3951                goto out;
3952        }
3953        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3954        memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3955        _scsih_setup_eedp(scmd, mpi_request);
3956        if (scmd->cmd_len == 32)
3957                mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3958        mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3959        if (sas_device_priv_data->sas_target->flags &
3960            MPT_TARGET_FLAGS_RAID_COMPONENT)
3961                mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3962        else
3963                mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3964        mpi_request->DevHandle =
3965            cpu_to_le16(sas_device_priv_data->sas_target->handle);
3966        mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3967        mpi_request->Control = cpu_to_le32(mpi_control);
3968        mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3969        mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3970        mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3971        mpi_request->SenseBufferLowAddress =
3972            mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3973        mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3974        mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3975            MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3976        mpi_request->VF_ID = 0; /* TODO */
3977        mpi_request->VP_ID = 0;
3978        int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3979            mpi_request->LUN);
3980        memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3981
3982        if (!mpi_request->DataLength) {
3983                mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3984        } else {
3985                if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3986                        mpt2sas_base_free_smid(ioc, smid);
3987                        goto out;
3988                }
3989        }
3990
3991        raid_device = sas_target_priv_data->raid_device;
3992        if (raid_device && raid_device->direct_io_enabled)
3993                _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3994                    smid);
3995
3996        if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3997                mpt2sas_base_put_smid_scsi_io(ioc, smid,
3998                    le16_to_cpu(mpi_request->DevHandle));
3999        else
4000                mpt2sas_base_put_smid_default(ioc, smid);
4001        return 0;
4002
4003 out:
4004        return SCSI_MLQUEUE_HOST_BUSY;
4005}
4006
4007static DEF_SCSI_QCMD(_scsih_qcmd)
4008
4009/**
4010 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4011 * @sense_buffer: sense data returned by target
4012 * @data: normalized skey/asc/ascq
4013 *
4014 * Return nothing.
4015 */
4016static void
4017_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4018{
4019        if ((sense_buffer[0] & 0x7F) >= 0x72) {
4020                /* descriptor format */
4021                data->skey = sense_buffer[1] & 0x0F;
4022                data->asc = sense_buffer[2];
4023                data->ascq = sense_buffer[3];
4024        } else {
4025                /* fixed format */
4026                data->skey = sense_buffer[2] & 0x0F;
4027                data->asc = sense_buffer[12];
4028                data->ascq = sense_buffer[13];
4029        }
4030}
4031
4032#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4033/**
4034 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
4035 * @ioc: per adapter object
4036 * @scmd: pointer to scsi command object
4037 * @mpi_reply: reply mf payload returned from firmware
4038 *
4039 * scsi_status - SCSI Status code returned from target device
4040 * scsi_state - state info associated with SCSI_IO determined by ioc
4041 * ioc_status - ioc supplied status info
4042 *
4043 * Return nothing.
4044 */
4045static void
4046_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4047    Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4048{
4049        u32 response_info;
4050        u8 *response_bytes;
4051        u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4052            MPI2_IOCSTATUS_MASK;
4053        u8 scsi_state = mpi_reply->SCSIState;
4054        u8 scsi_status = mpi_reply->SCSIStatus;
4055        char *desc_ioc_state = NULL;
4056        char *desc_scsi_status = NULL;
4057        char *desc_scsi_state = ioc->tmp_string;
4058        u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4059        struct _sas_device *sas_device = NULL;
4060        unsigned long flags;
4061        struct scsi_target *starget = scmd->device->sdev_target;
4062        struct MPT2SAS_TARGET *priv_target = starget->hostdata;
4063        char *device_str = NULL;
4064
4065        if (!priv_target)
4066                return;
4067
4068        if (ioc->hide_ir_msg)
4069                device_str = "WarpDrive";
4070        else
4071                device_str = "volume";
4072
4073        if (log_info == 0x31170000)
4074                return;
4075
4076        switch (ioc_status) {
4077        case MPI2_IOCSTATUS_SUCCESS:
4078                desc_ioc_state = "success";
4079                break;
4080        case MPI2_IOCSTATUS_INVALID_FUNCTION:
4081                desc_ioc_state = "invalid function";
4082                break;
4083        case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4084                desc_ioc_state = "scsi recovered error";
4085                break;
4086        case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4087                desc_ioc_state = "scsi invalid dev handle";
4088                break;
4089        case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4090                desc_ioc_state = "scsi device not there";
4091                break;
4092        case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4093                desc_ioc_state = "scsi data overrun";
4094                break;
4095        case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4096                desc_ioc_state = "scsi data underrun";
4097                break;
4098        case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4099                desc_ioc_state = "scsi io data error";
4100                break;
4101        case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4102                desc_ioc_state = "scsi protocol error";
4103                break;
4104        case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4105                desc_ioc_state = "scsi task terminated";
4106                break;
4107        case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4108                desc_ioc_state = "scsi residual mismatch";
4109                break;
4110        case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4111                desc_ioc_state = "scsi task mgmt failed";
4112                break;
4113        case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4114                desc_ioc_state = "scsi ioc terminated";
4115                break;
4116        case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4117                desc_ioc_state = "scsi ext terminated";
4118                break;
4119        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4120                desc_ioc_state = "eedp guard error";
4121                break;
4122        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4123                desc_ioc_state = "eedp ref tag error";
4124                break;
4125        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4126                desc_ioc_state = "eedp app tag error";
4127                break;
4128        default:
4129                desc_ioc_state = "unknown";
4130                break;
4131        }
4132
4133        switch (scsi_status) {
4134        case MPI2_SCSI_STATUS_GOOD:
4135                desc_scsi_status = "good";
4136                break;
4137        case MPI2_SCSI_STATUS_CHECK_CONDITION:
4138                desc_scsi_status = "check condition";
4139                break;
4140        case MPI2_SCSI_STATUS_CONDITION_MET:
4141                desc_scsi_status = "condition met";
4142                break;
4143        case MPI2_SCSI_STATUS_BUSY:
4144                desc_scsi_status = "busy";
4145                break;
4146        case MPI2_SCSI_STATUS_INTERMEDIATE:
4147                desc_scsi_status = "intermediate";
4148                break;
4149        case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4150                desc_scsi_status = "intermediate condmet";
4151                break;
4152        case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4153                desc_scsi_status = "reservation conflict";
4154                break;
4155        case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4156                desc_scsi_status = "command terminated";
4157                break;
4158        case MPI2_SCSI_STATUS_TASK_SET_FULL:
4159                desc_scsi_status = "task set full";
4160                break;
4161        case MPI2_SCSI_STATUS_ACA_ACTIVE:
4162                desc_scsi_status = "aca active";
4163                break;
4164        case MPI2_SCSI_STATUS_TASK_ABORTED:
4165                desc_scsi_status = "task aborted";
4166                break;
4167        default:
4168                desc_scsi_status = "unknown";
4169                break;
4170        }
4171
4172        desc_scsi_state[0] = '\0';
4173        if (!scsi_state)
4174                desc_scsi_state = " ";
4175        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4176                strcat(desc_scsi_state, "response info ");
4177        if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4178                strcat(desc_scsi_state, "state terminated ");
4179        if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4180                strcat(desc_scsi_state, "no status ");
4181        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4182                strcat(desc_scsi_state, "autosense failed ");
4183        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4184                strcat(desc_scsi_state, "autosense valid ");
4185
4186        scsi_print_command(scmd);
4187
4188        if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4189                printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4190                    device_str, (unsigned long long)priv_target->sas_address);
4191        } else {
4192                spin_lock_irqsave(&ioc->sas_device_lock, flags);
4193                sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4194                    priv_target->sas_address);
4195                if (sas_device) {
4196                        printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4197                            "phy(%d)\n", ioc->name, sas_device->sas_address,
4198                            sas_device->phy);
4199                        printk(MPT2SAS_WARN_FMT
4200                            "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4201                            ioc->name, sas_device->enclosure_logical_id,
4202                            sas_device->slot);
4203                }
4204                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4205        }
4206
4207        printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4208            "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4209            desc_ioc_state, ioc_status, smid);
4210        printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4211            "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4212            scsi_get_resid(scmd));
4213        printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4214            "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4215            le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4216        printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4217            "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4218            scsi_status, desc_scsi_state, scsi_state);
4219
4220        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4221                struct sense_info data;
4222                _scsih_normalize_sense(scmd->sense_buffer, &data);
4223                printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
4224                    "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4225                    data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4226        }
4227
4228        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4229                response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4230                response_bytes = (u8 *)&response_info;
4231                _scsih_response_code(ioc, response_bytes[0]);
4232        }
4233}
4234#endif
4235
4236/**
4237 * _scsih_turn_on_fault_led - illuminate Fault LED
4238 * @ioc: per adapter object
4239 * @handle: device handle
4240 * Context: process
4241 *
4242 * Return nothing.
4243 */
4244static void
4245_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4246{
4247        Mpi2SepReply_t mpi_reply;
4248        Mpi2SepRequest_t mpi_request;
4249
4250        memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4251        mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4252        mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4253        mpi_request.SlotStatus =
4254            cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4255        mpi_request.DevHandle = cpu_to_le16(handle);
4256        mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4257        if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4258            &mpi_request)) != 0) {
4259                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4260                __FILE__, __LINE__, __func__);
4261                return;
4262        }
4263
4264        if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4265                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4266                    "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4267                    le16_to_cpu(mpi_reply.IOCStatus),
4268                    le32_to_cpu(mpi_reply.IOCLogInfo)));
4269                return;
4270        }
4271}
4272
4273/**
4274 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4275 * @ioc: per adapter object
4276 * @handle: device handle
4277 * Context: interrupt.
4278 *
4279 * Return nothing.
4280 */
4281static void
4282_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4283{
4284        struct fw_event_work *fw_event;
4285
4286        fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4287        if (!fw_event)
4288                return;
4289        fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4290        fw_event->device_handle = handle;
4291        fw_event->ioc = ioc;
4292        _scsih_fw_event_add(ioc, fw_event);
4293}
4294
4295/**
4296 * _scsih_smart_predicted_fault - process smart errors
4297 * @ioc: per adapter object
4298 * @handle: device handle
4299 * Context: interrupt.
4300 *
4301 * Return nothing.
4302 */
4303static void
4304_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4305{
4306        struct scsi_target *starget;
4307        struct MPT2SAS_TARGET *sas_target_priv_data;
4308        Mpi2EventNotificationReply_t *event_reply;
4309        Mpi2EventDataSasDeviceStatusChange_t *event_data;
4310        struct _sas_device *sas_device;
4311        ssize_t sz;
4312        unsigned long flags;
4313
4314        /* only handle non-raid devices */
4315        spin_lock_irqsave(&ioc->sas_device_lock, flags);
4316        sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4317        if (!sas_device) {
4318                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4319                return;
4320        }
4321        starget = sas_device->starget;
4322        sas_target_priv_data = starget->hostdata;
4323
4324        if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4325           ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4326                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4327                return;
4328        }
4329        starget_printk(KERN_WARNING, starget, "predicted fault\n");
4330        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4331
4332        if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4333                _scsih_send_event_to_turn_on_fault_led(ioc, handle);
4334
4335        /* insert into event log */
4336        sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4337             sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4338        event_reply = kzalloc(sz, GFP_ATOMIC);
4339        if (!event_reply) {
4340                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4341                    ioc->name, __FILE__, __LINE__, __func__);
4342                return;
4343        }
4344
4345        event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4346        event_reply->Event =
4347            cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4348        event_reply->MsgLength = sz/4;
4349        event_reply->EventDataLength =
4350            cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4351        event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4352            event_reply->EventData;
4353        event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4354        event_data->ASC = 0x5D;
4355        event_data->DevHandle = cpu_to_le16(handle);
4356        event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4357        mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4358        kfree(event_reply);
4359}
4360
4361/**
4362 * _scsih_io_done - scsi request callback
4363 * @ioc: per adapter object
4364 * @smid: system request message index
4365 * @msix_index: MSIX table index supplied by the OS
4366 * @reply: reply message frame(lower 32bit addr)
4367 *
4368 * Callback handler when using _scsih_qcmd.
4369 *
4370 * Return 1 meaning mf should be freed from _base_interrupt
4371 *        0 means the mf is freed from this function.
4372 */
4373static u8
4374_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4375{
4376        Mpi2SCSIIORequest_t *mpi_request;
4377        Mpi2SCSIIOReply_t *mpi_reply;
4378        struct scsi_cmnd *scmd;
4379        u16 ioc_status;
4380        u32 xfer_cnt;
4381        u8 scsi_state;
4382        u8 scsi_status;
4383        u32 log_info;
4384        struct MPT2SAS_DEVICE *sas_device_priv_data;
4385        u32 response_code = 0;
4386        unsigned long flags;
4387
4388        mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4389        scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4390        if (scmd == NULL)
4391                return 1;
4392
4393        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4394
4395        if (mpi_reply == NULL) {
4396                scmd->result = DID_OK << 16;
4397                goto out;
4398        }
4399
4400        sas_device_priv_data = scmd->device->hostdata;
4401        if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4402             sas_device_priv_data->sas_target->deleted) {
4403                scmd->result = DID_NO_CONNECT << 16;
4404                goto out;
4405        }
4406        /*
4407         * WARPDRIVE: If direct_io is set then it is directIO,
4408         * the failed direct I/O should be redirected to volume
4409         */
4410        if (_scsih_scsi_direct_io_get(ioc, smid)) {
4411                spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4412                ioc->scsi_lookup[smid - 1].scmd = scmd;
4413                spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4414                _scsih_scsi_direct_io_set(ioc, smid, 0);
4415                memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4416                mpi_request->DevHandle =
4417                    cpu_to_le16(sas_device_priv_data->sas_target->handle);
4418                mpt2sas_base_put_smid_scsi_io(ioc, smid,
4419                    sas_device_priv_data->sas_target->handle);
4420                return 0;
4421        }
4422
4423
4424        /* turning off TLR */
4425        scsi_state = mpi_reply->SCSIState;
4426        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4427                response_code =
4428                    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4429        if (!sas_device_priv_data->tlr_snoop_check) {
4430                sas_device_priv_data->tlr_snoop_check++;
4431        /* Make sure Device is not raid volume.
4432         * We do not expose raid functionality to upper layer for warpdrive.
4433         */
4434        if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
4435                sas_is_tlr_enabled(scmd->device) &&
4436                    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4437                        sas_disable_tlr(scmd->device);
4438                        sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4439                }
4440        }
4441
4442        xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4443        scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4444        ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4445        if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4446                log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4447        else
4448                log_info = 0;
4449        ioc_status &= MPI2_IOCSTATUS_MASK;
4450        scsi_status = mpi_reply->SCSIStatus;
4451
4452        if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4453            (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4454             scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4455             scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4456                ioc_status = MPI2_IOCSTATUS_SUCCESS;
4457        }
4458
4459        if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4460                struct sense_info data;
4461                const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4462                    smid);
4463                u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4464                    le32_to_cpu(mpi_reply->SenseCount));
4465                memcpy(scmd->sense_buffer, sense_data, sz);
4466                _scsih_normalize_sense(scmd->sense_buffer, &data);
4467                /* failure prediction threshold exceeded */
4468                if (data.asc == 0x5D)
4469                        _scsih_smart_predicted_fault(ioc,
4470                            le16_to_cpu(mpi_reply->DevHandle));
4471        }
4472
4473        switch (ioc_status) {
4474        case MPI2_IOCSTATUS_BUSY:
4475        case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4476                scmd->result = SAM_STAT_BUSY;
4477                break;
4478
4479        case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4480                scmd->result = DID_NO_CONNECT << 16;
4481                break;
4482
4483        case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4484                if (sas_device_priv_data->block) {
4485                        scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4486                        goto out;
4487                }
4488        case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4489        case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4490                scmd->result = DID_RESET << 16;
4491                break;
4492
4493        case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4494                if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4495                        scmd->result = DID_SOFT_ERROR << 16;
4496                else
4497                        scmd->result = (DID_OK << 16) | scsi_status;
4498                break;
4499
4500        case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4501                scmd->result = (DID_OK << 16) | scsi_status;
4502
4503                if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4504                        break;
4505
4506                if (xfer_cnt < scmd->underflow) {
4507                        if (scsi_status == SAM_STAT_BUSY)
4508                                scmd->result = SAM_STAT_BUSY;
4509                        else
4510                                scmd->result = DID_SOFT_ERROR << 16;
4511                } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4512                     MPI2_SCSI_STATE_NO_SCSI_STATUS))
4513                        scmd->result = DID_SOFT_ERROR << 16;
4514                else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4515                        scmd->result = DID_RESET << 16;
4516                else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4517                        mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4518                        mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4519                        scmd->result = (DRIVER_SENSE << 24) |
4520                            SAM_STAT_CHECK_CONDITION;
4521                        scmd->sense_buffer[0] = 0x70;
4522                        scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4523                        scmd->sense_buffer[12] = 0x20;
4524                        scmd->sense_buffer[13] = 0;
4525                }
4526                break;
4527
4528        case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4529                scsi_set_resid(scmd, 0);
4530        case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4531        case MPI2_IOCSTATUS_SUCCESS:
4532                scmd->result = (DID_OK << 16) | scsi_status;
4533                if (response_code ==
4534                    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4535                    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4536                     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4537                        scmd->result = DID_SOFT_ERROR << 16;
4538                else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4539                        scmd->result = DID_RESET << 16;
4540                break;
4541
4542        case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4543        case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4544        case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4545                _scsih_eedp_error_handling(scmd, ioc_status);
4546                break;
4547        case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4548        case MPI2_IOCSTATUS_INVALID_FUNCTION:
4549        case MPI2_IOCSTATUS_INVALID_SGL:
4550        case MPI2_IOCSTATUS_INTERNAL_ERROR:
4551        case MPI2_IOCSTATUS_INVALID_FIELD:
4552        case MPI2_IOCSTATUS_INVALID_STATE:
4553        case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4554        case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4555        default:
4556                scmd->result = DID_SOFT_ERROR << 16;
4557                break;
4558
4559        }
4560
4561#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4562        if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4563                _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4564#endif
4565
4566 out:
4567        scsi_dma_unmap(scmd);
4568        scmd->scsi_done(scmd);
4569        return 1;
4570}
4571
4572/**
4573 * _scsih_sas_host_refresh - refreshing sas host object contents
4574 * @ioc: per adapter object
4575 * Context: user
4576 *
4577 * During port enable, fw will send topology events for every device. Its
4578 * possible that the handles may change from the previous setting, so this
4579 * code keeping handles updating if changed.
4580 *
4581 * Return nothing.
4582 */
4583static void
4584_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
4585{
4586        u16 sz;
4587        u16 ioc_status;
4588        int i;
4589        Mpi2ConfigReply_t mpi_reply;
4590        Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4591        u16 attached_handle;
4592        u8 link_rate;
4593
4594        dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4595            "updating handles for sas_host(0x%016llx)\n",
4596            ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4597
4598        sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4599            * sizeof(Mpi2SasIOUnit0PhyData_t));
4600        sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4601        if (!sas_iounit_pg0) {
4602                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4603                    ioc->name, __FILE__, __LINE__, __func__);
4604                return;
4605        }
4606
4607        if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4608            sas_iounit_pg0, sz)) != 0)
4609                goto out;
4610        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4611        if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4612                goto out;
4613        for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4614                link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4615                if (i == 0)
4616                        ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4617                            PhyData[0].ControllerDevHandle);
4618                ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4619                attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4620                    AttachedDevHandle);
4621                if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4622                        link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4623                mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4624                    attached_handle, i, link_rate);
4625        }
4626 out:
4627        kfree(sas_iounit_pg0);
4628}
4629
4630/**
4631 * _scsih_sas_host_add - create sas host object
4632 * @ioc: per adapter object
4633 *
4634 * Creating host side data object, stored in ioc->sas_hba
4635 *
4636 * Return nothing.
4637 */
4638static void
4639_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4640{
4641        int i;
4642        Mpi2ConfigReply_t mpi_reply;
4643        Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4644        Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4645        Mpi2SasPhyPage0_t phy_pg0;
4646        Mpi2SasDevicePage0_t sas_device_pg0;
4647        Mpi2SasEnclosurePage0_t enclosure_pg0;
4648        u16 ioc_status;
4649        u16 sz;
4650        u16 device_missing_delay;
4651
4652        mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4653        if (!ioc->sas_hba.num_phys) {
4654                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4655                    ioc->name, __FILE__, __LINE__, __func__);
4656                return;
4657        }
4658
4659        /* sas_iounit page 0 */
4660        sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4661            sizeof(Mpi2SasIOUnit0PhyData_t));
4662        sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4663        if (!sas_iounit_pg0) {
4664                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4665                    ioc->name, __FILE__, __LINE__, __func__);
4666                return;
4667        }
4668        if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4669            sas_iounit_pg0, sz))) {
4670                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4671                    ioc->name, __FILE__, __LINE__, __func__);
4672                goto out;
4673        }
4674        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4675            MPI2_IOCSTATUS_MASK;
4676        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4677                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4678                    ioc->name, __FILE__, __LINE__, __func__);
4679                goto out;
4680        }
4681
4682        /* sas_iounit page 1 */
4683        sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4684            sizeof(Mpi2SasIOUnit1PhyData_t));
4685        sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4686        if (!sas_iounit_pg1) {
4687                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4688                    ioc->name, __FILE__, __LINE__, __func__);
4689                goto out;
4690        }
4691        if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4692            sas_iounit_pg1, sz))) {
4693                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4694                    ioc->name, __FILE__, __LINE__, __func__);
4695                goto out;
4696        }
4697        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4698            MPI2_IOCSTATUS_MASK;
4699        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4700                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4701                    ioc->name, __FILE__, __LINE__, __func__);
4702                goto out;
4703        }
4704
4705        ioc->io_missing_delay =
4706            le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4707        device_missing_delay =
4708            le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4709        if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4710                ioc->device_missing_delay = (device_missing_delay &
4711                    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4712        else
4713                ioc->device_missing_delay = device_missing_delay &
4714                    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4715
4716        ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4717        ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4718            sizeof(struct _sas_phy), GFP_KERNEL);
4719        if (!ioc->sas_hba.phy) {
4720                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4721                    ioc->name, __FILE__, __LINE__, __func__);
4722                goto out;
4723        }
4724        for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4725                if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4726                    i))) {
4727                        printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4728                            ioc->name, __FILE__, __LINE__, __func__);
4729                        goto out;
4730                }
4731                ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4732                    MPI2_IOCSTATUS_MASK;
4733                if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4734                        printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4735                            ioc->name, __FILE__, __LINE__, __func__);
4736                        goto out;
4737                }
4738
4739                if (i == 0)
4740                        ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4741                            PhyData[0].ControllerDevHandle);
4742                ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4743                ioc->sas_hba.phy[i].phy_id = i;
4744                mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4745                    phy_pg0, ioc->sas_hba.parent_dev);
4746        }
4747        if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4748            MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4749                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4750                    ioc->name, __FILE__, __LINE__, __func__);
4751                goto out;
4752        }
4753        ioc->sas_hba.enclosure_handle =
4754            le16_to_cpu(sas_device_pg0.EnclosureHandle);
4755        ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4756        printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4757            "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4758            (unsigned long long) ioc->sas_hba.sas_address,
4759            ioc->sas_hba.num_phys) ;
4760
4761        if (ioc->sas_hba.enclosure_handle) {
4762                if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4763                    &enclosure_pg0,
4764                   MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4765                   ioc->sas_hba.enclosure_handle))) {
4766                        ioc->sas_hba.enclosure_logical_id =
4767                            le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4768                }
4769        }
4770
4771 out:
4772        kfree(sas_iounit_pg1);
4773        kfree(sas_iounit_pg0);
4774}
4775
4776/**
4777 * _scsih_expander_add -  creating expander object
4778 * @ioc: per adapter object
4779 * @handle: expander handle
4780 *
4781 * Creating expander object, stored in ioc->sas_expander_list.
4782 *
4783 * Return 0 for success, else error.
4784 */
4785static int
4786_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4787{
4788        struct _sas_node *sas_expander;
4789        Mpi2ConfigReply_t mpi_reply;
4790        Mpi2ExpanderPage0_t expander_pg0;
4791        Mpi2ExpanderPage1_t expander_pg1;
4792        Mpi2SasEnclosurePage0_t enclosure_pg0;
4793        u32 ioc_status;
4794        u16 parent_handle;
4795        u64 sas_address, sas_address_parent = 0;
4796        int i;
4797        unsigned long flags;
4798        struct _sas_port *mpt2sas_port = NULL;
4799        int rc = 0;
4800
4801        if (!handle)
4802                return -1;
4803
4804        if (ioc->shost_recovery || ioc->pci_error_recovery)
4805                return -1;
4806
4807        if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4808            MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4809                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4810                    ioc->name, __FILE__, __LINE__, __func__);
4811                return -1;
4812        }
4813
4814        ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4815            MPI2_IOCSTATUS_MASK;
4816        if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4817                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4818                    ioc->name, __FILE__, __LINE__, __func__);
4819                return -1;
4820        }
4821
4822        /* handle out of order topology events */
4823        parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4824        if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4825            != 0) {
4826                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4827                    ioc->name, __FILE__, __LINE__, __func__);
4828                return -1;
4829        }
4830        if (sas_address_parent != ioc->sas_hba.sas_address) {
4831                spin_lock_irqsave(&ioc->sas_node_lock, flags);
4832                sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4833                    sas_address_parent);
4834                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4835                if (!sas_expander) {
4836                        rc = _scsih_expander_add(ioc, parent_handle);
4837                        if (rc != 0)
4838                                return rc;
4839                }
4840        }
4841
4842        spin_lock_irqsave(&ioc->sas_node_lock, flags);
4843        sas_address = le64_to_cpu(expander_pg0.SASAddress);
4844        sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4845            sas_address);
4846        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4847
4848        if (sas_expander)
4849                return 0;