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