linux/drivers/scsi/megaraid/megaraid_sas_base.c
<<
>>
Prefs
   1/*
   2 *  Linux MegaRAID driver for SAS based RAID controllers
   3 *
   4 *  Copyright (c) 2003-2012  LSI Corporation.
   5 *
   6 *  This program is free software; you can redistribute it and/or
   7 *  modify it under the terms of the GNU General Public License
   8 *  as published by the Free Software Foundation; either version 2
   9 *  of the License, or (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *  GNU General Public License for more details.
  15 *
  16 *  You should have received a copy of the GNU General Public License
  17 *  along with this program; if not, write to the Free Software
  18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 *
  20 *  FILE: megaraid_sas_base.c
  21 *  Version : v06.506.00.00-rc1
  22 *
  23 *  Authors: LSI Corporation
  24 *           Sreenivas Bagalkote
  25 *           Sumant Patro
  26 *           Bo Yang
  27 *           Adam Radford <linuxraid@lsi.com>
  28 *
  29 *  Send feedback to: <megaraidlinux@lsi.com>
  30 *
  31 *  Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  32 *     ATTN: Linuxraid
  33 */
  34
  35#include <linux/kernel.h>
  36#include <linux/types.h>
  37#include <linux/pci.h>
  38#include <linux/list.h>
  39#include <linux/moduleparam.h>
  40#include <linux/module.h>
  41#include <linux/spinlock.h>
  42#include <linux/interrupt.h>
  43#include <linux/delay.h>
  44#include <linux/uio.h>
  45#include <linux/slab.h>
  46#include <asm/uaccess.h>
  47#include <linux/fs.h>
  48#include <linux/compat.h>
  49#include <linux/blkdev.h>
  50#include <linux/mutex.h>
  51#include <linux/poll.h>
  52
  53#include <scsi/scsi.h>
  54#include <scsi/scsi_cmnd.h>
  55#include <scsi/scsi_device.h>
  56#include <scsi/scsi_host.h>
  57#include <scsi/scsi_tcq.h>
  58#include "megaraid_sas_fusion.h"
  59#include "megaraid_sas.h"
  60
  61/*
  62 * Number of sectors per IO command
  63 * Will be set in megasas_init_mfi if user does not provide
  64 */
  65static unsigned int max_sectors;
  66module_param_named(max_sectors, max_sectors, int, 0);
  67MODULE_PARM_DESC(max_sectors,
  68        "Maximum number of sectors per IO command");
  69
  70static int msix_disable;
  71module_param(msix_disable, int, S_IRUGO);
  72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  73
  74static unsigned int msix_vectors;
  75module_param(msix_vectors, int, S_IRUGO);
  76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  77
  78static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  79module_param(throttlequeuedepth, int, S_IRUGO);
  80MODULE_PARM_DESC(throttlequeuedepth,
  81        "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  82
  83int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  84module_param(resetwaittime, int, S_IRUGO);
  85MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
  86                 "before resetting adapter. Default: 180");
  87
  88MODULE_LICENSE("GPL");
  89MODULE_VERSION(MEGASAS_VERSION);
  90MODULE_AUTHOR("megaraidlinux@lsi.com");
  91MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
  92
  93int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  94static int megasas_get_pd_list(struct megasas_instance *instance);
  95static int megasas_issue_init_mfi(struct megasas_instance *instance);
  96static int megasas_register_aen(struct megasas_instance *instance,
  97                                u32 seq_num, u32 class_locale_word);
  98/*
  99 * PCI ID table for all supported controllers
 100 */
 101static struct pci_device_id megasas_pci_table[] = {
 102
 103        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
 104        /* xscale IOP */
 105        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
 106        /* ppc IOP */
 107        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
 108        /* ppc IOP */
 109        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
 110        /* gen2*/
 111        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
 112        /* gen2*/
 113        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
 114        /* skinny*/
 115        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
 116        /* skinny*/
 117        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
 118        /* xscale IOP, vega */
 119        {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
 120        /* xscale IOP */
 121        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
 122        /* Fusion */
 123        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
 124        /* Invader */
 125        {}
 126};
 127
 128MODULE_DEVICE_TABLE(pci, megasas_pci_table);
 129
 130static int megasas_mgmt_majorno;
 131static struct megasas_mgmt_info megasas_mgmt_info;
 132static struct fasync_struct *megasas_async_queue;
 133static DEFINE_MUTEX(megasas_async_queue_mutex);
 134
 135static int megasas_poll_wait_aen;
 136static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
 137static u32 support_poll_for_event;
 138u32 megasas_dbg_lvl;
 139static u32 support_device_change;
 140
 141/* define lock for aen poll */
 142spinlock_t poll_aen_lock;
 143
 144void
 145megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
 146                     u8 alt_status);
 147static u32
 148megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
 149static int
 150megasas_adp_reset_gen2(struct megasas_instance *instance,
 151                       struct megasas_register_set __iomem *reg_set);
 152static irqreturn_t megasas_isr(int irq, void *devp);
 153static u32
 154megasas_init_adapter_mfi(struct megasas_instance *instance);
 155u32
 156megasas_build_and_issue_cmd(struct megasas_instance *instance,
 157                            struct scsi_cmnd *scmd);
 158static void megasas_complete_cmd_dpc(unsigned long instance_addr);
 159void
 160megasas_release_fusion(struct megasas_instance *instance);
 161int
 162megasas_ioc_init_fusion(struct megasas_instance *instance);
 163void
 164megasas_free_cmds_fusion(struct megasas_instance *instance);
 165u8
 166megasas_get_map_info(struct megasas_instance *instance);
 167int
 168megasas_sync_map_info(struct megasas_instance *instance);
 169int
 170wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
 171void megasas_reset_reply_desc(struct megasas_instance *instance);
 172u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
 173                      struct LD_LOAD_BALANCE_INFO *lbInfo);
 174int megasas_reset_fusion(struct Scsi_Host *shost);
 175void megasas_fusion_ocr_wq(struct work_struct *work);
 176
 177void
 178megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 179{
 180        instance->instancet->fire_cmd(instance,
 181                cmd->frame_phys_addr, 0, instance->reg_set);
 182}
 183
 184/**
 185 * megasas_get_cmd -    Get a command from the free pool
 186 * @instance:           Adapter soft state
 187 *
 188 * Returns a free command from the pool
 189 */
 190struct megasas_cmd *megasas_get_cmd(struct megasas_instance
 191                                                  *instance)
 192{
 193        unsigned long flags;
 194        struct megasas_cmd *cmd = NULL;
 195
 196        spin_lock_irqsave(&instance->cmd_pool_lock, flags);
 197
 198        if (!list_empty(&instance->cmd_pool)) {
 199                cmd = list_entry((&instance->cmd_pool)->next,
 200                                 struct megasas_cmd, list);
 201                list_del_init(&cmd->list);
 202        } else {
 203                printk(KERN_ERR "megasas: Command pool empty!\n");
 204        }
 205
 206        spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
 207        return cmd;
 208}
 209
 210/**
 211 * megasas_return_cmd - Return a cmd to free command pool
 212 * @instance:           Adapter soft state
 213 * @cmd:                Command packet to be returned to free command pool
 214 */
 215inline void
 216megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 217{
 218        unsigned long flags;
 219
 220        spin_lock_irqsave(&instance->cmd_pool_lock, flags);
 221
 222        cmd->scmd = NULL;
 223        cmd->frame_count = 0;
 224        if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
 225            (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
 226            (reset_devices))
 227                cmd->frame->hdr.cmd = MFI_CMD_INVALID;
 228        list_add_tail(&cmd->list, &instance->cmd_pool);
 229
 230        spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
 231}
 232
 233
 234/**
 235*       The following functions are defined for xscale
 236*       (deviceid : 1064R, PERC5) controllers
 237*/
 238
 239/**
 240 * megasas_enable_intr_xscale - Enables interrupts
 241 * @regs:                       MFI register set
 242 */
 243static inline void
 244megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
 245{
 246        writel(0, &(regs)->outbound_intr_mask);
 247
 248        /* Dummy readl to force pci flush */
 249        readl(&regs->outbound_intr_mask);
 250}
 251
 252/**
 253 * megasas_disable_intr_xscale -Disables interrupt
 254 * @regs:                       MFI register set
 255 */
 256static inline void
 257megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
 258{
 259        u32 mask = 0x1f;
 260        writel(mask, &regs->outbound_intr_mask);
 261        /* Dummy readl to force pci flush */
 262        readl(&regs->outbound_intr_mask);
 263}
 264
 265/**
 266 * megasas_read_fw_status_reg_xscale - returns the current FW status value
 267 * @regs:                       MFI register set
 268 */
 269static u32
 270megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
 271{
 272        return readl(&(regs)->outbound_msg_0);
 273}
 274/**
 275 * megasas_clear_interrupt_xscale -     Check & clear interrupt
 276 * @regs:                               MFI register set
 277 */
 278static int
 279megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
 280{
 281        u32 status;
 282        u32 mfiStatus = 0;
 283        /*
 284         * Check if it is our interrupt
 285         */
 286        status = readl(&regs->outbound_intr_status);
 287
 288        if (status & MFI_OB_INTR_STATUS_MASK)
 289                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 290        if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
 291                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 292
 293        /*
 294         * Clear the interrupt by writing back the same value
 295         */
 296        if (mfiStatus)
 297                writel(status, &regs->outbound_intr_status);
 298
 299        /* Dummy readl to force pci flush */
 300        readl(&regs->outbound_intr_status);
 301
 302        return mfiStatus;
 303}
 304
 305/**
 306 * megasas_fire_cmd_xscale -    Sends command to the FW
 307 * @frame_phys_addr :           Physical address of cmd
 308 * @frame_count :               Number of frames for the command
 309 * @regs :                      MFI register set
 310 */
 311static inline void
 312megasas_fire_cmd_xscale(struct megasas_instance *instance,
 313                dma_addr_t frame_phys_addr,
 314                u32 frame_count,
 315                struct megasas_register_set __iomem *regs)
 316{
 317        unsigned long flags;
 318        spin_lock_irqsave(&instance->hba_lock, flags);
 319        writel((frame_phys_addr >> 3)|(frame_count),
 320               &(regs)->inbound_queue_port);
 321        spin_unlock_irqrestore(&instance->hba_lock, flags);
 322}
 323
 324/**
 325 * megasas_adp_reset_xscale -  For controller reset
 326 * @regs:                              MFI register set
 327 */
 328static int
 329megasas_adp_reset_xscale(struct megasas_instance *instance,
 330        struct megasas_register_set __iomem *regs)
 331{
 332        u32 i;
 333        u32 pcidata;
 334        writel(MFI_ADP_RESET, &regs->inbound_doorbell);
 335
 336        for (i = 0; i < 3; i++)
 337                msleep(1000); /* sleep for 3 secs */
 338        pcidata  = 0;
 339        pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
 340        printk(KERN_NOTICE "pcidata = %x\n", pcidata);
 341        if (pcidata & 0x2) {
 342                printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
 343                pcidata &= ~0x2;
 344                pci_write_config_dword(instance->pdev,
 345                                MFI_1068_PCSR_OFFSET, pcidata);
 346
 347                for (i = 0; i < 2; i++)
 348                        msleep(1000); /* need to wait 2 secs again */
 349
 350                pcidata  = 0;
 351                pci_read_config_dword(instance->pdev,
 352                                MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
 353                printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
 354                if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
 355                        printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
 356                        pcidata = 0;
 357                        pci_write_config_dword(instance->pdev,
 358                                MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
 359                }
 360        }
 361        return 0;
 362}
 363
 364/**
 365 * megasas_check_reset_xscale - For controller reset check
 366 * @regs:                               MFI register set
 367 */
 368static int
 369megasas_check_reset_xscale(struct megasas_instance *instance,
 370                struct megasas_register_set __iomem *regs)
 371{
 372        u32 consumer;
 373        consumer = *instance->consumer;
 374
 375        if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
 376                (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
 377                return 1;
 378        }
 379        return 0;
 380}
 381
 382static struct megasas_instance_template megasas_instance_template_xscale = {
 383
 384        .fire_cmd = megasas_fire_cmd_xscale,
 385        .enable_intr = megasas_enable_intr_xscale,
 386        .disable_intr = megasas_disable_intr_xscale,
 387        .clear_intr = megasas_clear_intr_xscale,
 388        .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
 389        .adp_reset = megasas_adp_reset_xscale,
 390        .check_reset = megasas_check_reset_xscale,
 391        .service_isr = megasas_isr,
 392        .tasklet = megasas_complete_cmd_dpc,
 393        .init_adapter = megasas_init_adapter_mfi,
 394        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 395        .issue_dcmd = megasas_issue_dcmd,
 396};
 397
 398/**
 399*       This is the end of set of functions & definitions specific
 400*       to xscale (deviceid : 1064R, PERC5) controllers
 401*/
 402
 403/**
 404*       The following functions are defined for ppc (deviceid : 0x60)
 405*       controllers
 406*/
 407
 408/**
 409 * megasas_enable_intr_ppc -    Enables interrupts
 410 * @regs:                       MFI register set
 411 */
 412static inline void
 413megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
 414{
 415        writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 416
 417        writel(~0x80000000, &(regs)->outbound_intr_mask);
 418
 419        /* Dummy readl to force pci flush */
 420        readl(&regs->outbound_intr_mask);
 421}
 422
 423/**
 424 * megasas_disable_intr_ppc -   Disable interrupt
 425 * @regs:                       MFI register set
 426 */
 427static inline void
 428megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
 429{
 430        u32 mask = 0xFFFFFFFF;
 431        writel(mask, &regs->outbound_intr_mask);
 432        /* Dummy readl to force pci flush */
 433        readl(&regs->outbound_intr_mask);
 434}
 435
 436/**
 437 * megasas_read_fw_status_reg_ppc - returns the current FW status value
 438 * @regs:                       MFI register set
 439 */
 440static u32
 441megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
 442{
 443        return readl(&(regs)->outbound_scratch_pad);
 444}
 445
 446/**
 447 * megasas_clear_interrupt_ppc -        Check & clear interrupt
 448 * @regs:                               MFI register set
 449 */
 450static int
 451megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
 452{
 453        u32 status, mfiStatus = 0;
 454
 455        /*
 456         * Check if it is our interrupt
 457         */
 458        status = readl(&regs->outbound_intr_status);
 459
 460        if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
 461                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 462
 463        if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
 464                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 465
 466        /*
 467         * Clear the interrupt by writing back the same value
 468         */
 469        writel(status, &regs->outbound_doorbell_clear);
 470
 471        /* Dummy readl to force pci flush */
 472        readl(&regs->outbound_doorbell_clear);
 473
 474        return mfiStatus;
 475}
 476
 477/**
 478 * megasas_fire_cmd_ppc -       Sends command to the FW
 479 * @frame_phys_addr :           Physical address of cmd
 480 * @frame_count :               Number of frames for the command
 481 * @regs :                      MFI register set
 482 */
 483static inline void
 484megasas_fire_cmd_ppc(struct megasas_instance *instance,
 485                dma_addr_t frame_phys_addr,
 486                u32 frame_count,
 487                struct megasas_register_set __iomem *regs)
 488{
 489        unsigned long flags;
 490        spin_lock_irqsave(&instance->hba_lock, flags);
 491        writel((frame_phys_addr | (frame_count<<1))|1,
 492                        &(regs)->inbound_queue_port);
 493        spin_unlock_irqrestore(&instance->hba_lock, flags);
 494}
 495
 496/**
 497 * megasas_check_reset_ppc -    For controller reset check
 498 * @regs:                               MFI register set
 499 */
 500static int
 501megasas_check_reset_ppc(struct megasas_instance *instance,
 502                        struct megasas_register_set __iomem *regs)
 503{
 504        if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
 505                return 1;
 506
 507        return 0;
 508}
 509
 510static struct megasas_instance_template megasas_instance_template_ppc = {
 511
 512        .fire_cmd = megasas_fire_cmd_ppc,
 513        .enable_intr = megasas_enable_intr_ppc,
 514        .disable_intr = megasas_disable_intr_ppc,
 515        .clear_intr = megasas_clear_intr_ppc,
 516        .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
 517        .adp_reset = megasas_adp_reset_xscale,
 518        .check_reset = megasas_check_reset_ppc,
 519        .service_isr = megasas_isr,
 520        .tasklet = megasas_complete_cmd_dpc,
 521        .init_adapter = megasas_init_adapter_mfi,
 522        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 523        .issue_dcmd = megasas_issue_dcmd,
 524};
 525
 526/**
 527 * megasas_enable_intr_skinny - Enables interrupts
 528 * @regs:                       MFI register set
 529 */
 530static inline void
 531megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
 532{
 533        writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
 534
 535        writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 536
 537        /* Dummy readl to force pci flush */
 538        readl(&regs->outbound_intr_mask);
 539}
 540
 541/**
 542 * megasas_disable_intr_skinny -        Disables interrupt
 543 * @regs:                       MFI register set
 544 */
 545static inline void
 546megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
 547{
 548        u32 mask = 0xFFFFFFFF;
 549        writel(mask, &regs->outbound_intr_mask);
 550        /* Dummy readl to force pci flush */
 551        readl(&regs->outbound_intr_mask);
 552}
 553
 554/**
 555 * megasas_read_fw_status_reg_skinny - returns the current FW status value
 556 * @regs:                       MFI register set
 557 */
 558static u32
 559megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
 560{
 561        return readl(&(regs)->outbound_scratch_pad);
 562}
 563
 564/**
 565 * megasas_clear_interrupt_skinny -     Check & clear interrupt
 566 * @regs:                               MFI register set
 567 */
 568static int
 569megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
 570{
 571        u32 status;
 572        u32 mfiStatus = 0;
 573
 574        /*
 575         * Check if it is our interrupt
 576         */
 577        status = readl(&regs->outbound_intr_status);
 578
 579        if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
 580                return 0;
 581        }
 582
 583        /*
 584         * Check if it is our interrupt
 585         */
 586        if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
 587            MFI_STATE_FAULT) {
 588                mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 589        } else
 590                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 591
 592        /*
 593         * Clear the interrupt by writing back the same value
 594         */
 595        writel(status, &regs->outbound_intr_status);
 596
 597        /*
 598        * dummy read to flush PCI
 599        */
 600        readl(&regs->outbound_intr_status);
 601
 602        return mfiStatus;
 603}
 604
 605/**
 606 * megasas_fire_cmd_skinny -    Sends command to the FW
 607 * @frame_phys_addr :           Physical address of cmd
 608 * @frame_count :               Number of frames for the command
 609 * @regs :                      MFI register set
 610 */
 611static inline void
 612megasas_fire_cmd_skinny(struct megasas_instance *instance,
 613                        dma_addr_t frame_phys_addr,
 614                        u32 frame_count,
 615                        struct megasas_register_set __iomem *regs)
 616{
 617        unsigned long flags;
 618        spin_lock_irqsave(&instance->hba_lock, flags);
 619        writel(0, &(regs)->inbound_high_queue_port);
 620        writel((frame_phys_addr | (frame_count<<1))|1,
 621                &(regs)->inbound_low_queue_port);
 622        spin_unlock_irqrestore(&instance->hba_lock, flags);
 623}
 624
 625/**
 626 * megasas_check_reset_skinny - For controller reset check
 627 * @regs:                               MFI register set
 628 */
 629static int
 630megasas_check_reset_skinny(struct megasas_instance *instance,
 631                                struct megasas_register_set __iomem *regs)
 632{
 633        if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
 634                return 1;
 635
 636        return 0;
 637}
 638
 639static struct megasas_instance_template megasas_instance_template_skinny = {
 640
 641        .fire_cmd = megasas_fire_cmd_skinny,
 642        .enable_intr = megasas_enable_intr_skinny,
 643        .disable_intr = megasas_disable_intr_skinny,
 644        .clear_intr = megasas_clear_intr_skinny,
 645        .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
 646        .adp_reset = megasas_adp_reset_gen2,
 647        .check_reset = megasas_check_reset_skinny,
 648        .service_isr = megasas_isr,
 649        .tasklet = megasas_complete_cmd_dpc,
 650        .init_adapter = megasas_init_adapter_mfi,
 651        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 652        .issue_dcmd = megasas_issue_dcmd,
 653};
 654
 655
 656/**
 657*       The following functions are defined for gen2 (deviceid : 0x78 0x79)
 658*       controllers
 659*/
 660
 661/**
 662 * megasas_enable_intr_gen2 -  Enables interrupts
 663 * @regs:                      MFI register set
 664 */
 665static inline void
 666megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
 667{
 668        writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 669
 670        /* write ~0x00000005 (4 & 1) to the intr mask*/
 671        writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 672
 673        /* Dummy readl to force pci flush */
 674        readl(&regs->outbound_intr_mask);
 675}
 676
 677/**
 678 * megasas_disable_intr_gen2 - Disables interrupt
 679 * @regs:                      MFI register set
 680 */
 681static inline void
 682megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
 683{
 684        u32 mask = 0xFFFFFFFF;
 685        writel(mask, &regs->outbound_intr_mask);
 686        /* Dummy readl to force pci flush */
 687        readl(&regs->outbound_intr_mask);
 688}
 689
 690/**
 691 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
 692 * @regs:                      MFI register set
 693 */
 694static u32
 695megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
 696{
 697        return readl(&(regs)->outbound_scratch_pad);
 698}
 699
 700/**
 701 * megasas_clear_interrupt_gen2 -      Check & clear interrupt
 702 * @regs:                              MFI register set
 703 */
 704static int
 705megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
 706{
 707        u32 status;
 708        u32 mfiStatus = 0;
 709        /*
 710         * Check if it is our interrupt
 711         */
 712        status = readl(&regs->outbound_intr_status);
 713
 714        if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
 715                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 716        }
 717        if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
 718                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 719        }
 720
 721        /*
 722         * Clear the interrupt by writing back the same value
 723         */
 724        if (mfiStatus)
 725                writel(status, &regs->outbound_doorbell_clear);
 726
 727        /* Dummy readl to force pci flush */
 728        readl(&regs->outbound_intr_status);
 729
 730        return mfiStatus;
 731}
 732/**
 733 * megasas_fire_cmd_gen2 -     Sends command to the FW
 734 * @frame_phys_addr :          Physical address of cmd
 735 * @frame_count :              Number of frames for the command
 736 * @regs :                     MFI register set
 737 */
 738static inline void
 739megasas_fire_cmd_gen2(struct megasas_instance *instance,
 740                        dma_addr_t frame_phys_addr,
 741                        u32 frame_count,
 742                        struct megasas_register_set __iomem *regs)
 743{
 744        unsigned long flags;
 745        spin_lock_irqsave(&instance->hba_lock, flags);
 746        writel((frame_phys_addr | (frame_count<<1))|1,
 747                        &(regs)->inbound_queue_port);
 748        spin_unlock_irqrestore(&instance->hba_lock, flags);
 749}
 750
 751/**
 752 * megasas_adp_reset_gen2 -     For controller reset
 753 * @regs:                               MFI register set
 754 */
 755static int
 756megasas_adp_reset_gen2(struct megasas_instance *instance,
 757                        struct megasas_register_set __iomem *reg_set)
 758{
 759        u32                     retry = 0 ;
 760        u32                     HostDiag;
 761        u32                     *seq_offset = &reg_set->seq_offset;
 762        u32                     *hostdiag_offset = &reg_set->host_diag;
 763
 764        if (instance->instancet == &megasas_instance_template_skinny) {
 765                seq_offset = &reg_set->fusion_seq_offset;
 766                hostdiag_offset = &reg_set->fusion_host_diag;
 767        }
 768
 769        writel(0, seq_offset);
 770        writel(4, seq_offset);
 771        writel(0xb, seq_offset);
 772        writel(2, seq_offset);
 773        writel(7, seq_offset);
 774        writel(0xd, seq_offset);
 775
 776        msleep(1000);
 777
 778        HostDiag = (u32)readl(hostdiag_offset);
 779
 780        while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
 781                msleep(100);
 782                HostDiag = (u32)readl(hostdiag_offset);
 783                printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
 784                                        retry, HostDiag);
 785
 786                if (retry++ >= 100)
 787                        return 1;
 788
 789        }
 790
 791        printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
 792
 793        writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
 794
 795        ssleep(10);
 796
 797        HostDiag = (u32)readl(hostdiag_offset);
 798        while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
 799                msleep(100);
 800                HostDiag = (u32)readl(hostdiag_offset);
 801                printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
 802                                retry, HostDiag);
 803
 804                if (retry++ >= 1000)
 805                        return 1;
 806
 807        }
 808        return 0;
 809}
 810
 811/**
 812 * megasas_check_reset_gen2 -   For controller reset check
 813 * @regs:                               MFI register set
 814 */
 815static int
 816megasas_check_reset_gen2(struct megasas_instance *instance,
 817                struct megasas_register_set __iomem *regs)
 818{
 819        if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
 820                return 1;
 821        }
 822
 823        return 0;
 824}
 825
 826static struct megasas_instance_template megasas_instance_template_gen2 = {
 827
 828        .fire_cmd = megasas_fire_cmd_gen2,
 829        .enable_intr = megasas_enable_intr_gen2,
 830        .disable_intr = megasas_disable_intr_gen2,
 831        .clear_intr = megasas_clear_intr_gen2,
 832        .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
 833        .adp_reset = megasas_adp_reset_gen2,
 834        .check_reset = megasas_check_reset_gen2,
 835        .service_isr = megasas_isr,
 836        .tasklet = megasas_complete_cmd_dpc,
 837        .init_adapter = megasas_init_adapter_mfi,
 838        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 839        .issue_dcmd = megasas_issue_dcmd,
 840};
 841
 842/**
 843*       This is the end of set of functions & definitions
 844*       specific to gen2 (deviceid : 0x78, 0x79) controllers
 845*/
 846
 847/*
 848 * Template added for TB (Fusion)
 849 */
 850extern struct megasas_instance_template megasas_instance_template_fusion;
 851
 852/**
 853 * megasas_issue_polled -       Issues a polling command
 854 * @instance:                   Adapter soft state
 855 * @cmd:                        Command packet to be issued
 856 *
 857 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
 858 */
 859int
 860megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
 861{
 862
 863        struct megasas_header *frame_hdr = &cmd->frame->hdr;
 864
 865        frame_hdr->cmd_status = 0xFF;
 866        frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
 867
 868        /*
 869         * Issue the frame using inbound queue port
 870         */
 871        instance->instancet->issue_dcmd(instance, cmd);
 872
 873        /*
 874         * Wait for cmd_status to change
 875         */
 876        return wait_and_poll(instance, cmd);
 877}
 878
 879/**
 880 * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
 881 * @instance:                   Adapter soft state
 882 * @cmd:                        Command to be issued
 883 *
 884 * This function waits on an event for the command to be returned from ISR.
 885 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
 886 * Used to issue ioctl commands.
 887 */
 888static int
 889megasas_issue_blocked_cmd(struct megasas_instance *instance,
 890                          struct megasas_cmd *cmd)
 891{
 892        cmd->cmd_status = ENODATA;
 893
 894        instance->instancet->issue_dcmd(instance, cmd);
 895
 896        wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
 897
 898        return 0;
 899}
 900
 901/**
 902 * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
 903 * @instance:                           Adapter soft state
 904 * @cmd_to_abort:                       Previously issued cmd to be aborted
 905 *
 906 * MFI firmware can abort previously issued AEN command (automatic event
 907 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
 908 * cmd and waits for return status.
 909 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
 910 */
 911static int
 912megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
 913                                struct megasas_cmd *cmd_to_abort)
 914{
 915        struct megasas_cmd *cmd;
 916        struct megasas_abort_frame *abort_fr;
 917
 918        cmd = megasas_get_cmd(instance);
 919
 920        if (!cmd)
 921                return -1;
 922
 923        abort_fr = &cmd->frame->abort;
 924
 925        /*
 926         * Prepare and issue the abort frame
 927         */
 928        abort_fr->cmd = MFI_CMD_ABORT;
 929        abort_fr->cmd_status = 0xFF;
 930        abort_fr->flags = 0;
 931        abort_fr->abort_context = cmd_to_abort->index;
 932        abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
 933        abort_fr->abort_mfi_phys_addr_hi = 0;
 934
 935        cmd->sync_cmd = 1;
 936        cmd->cmd_status = 0xFF;
 937
 938        instance->instancet->issue_dcmd(instance, cmd);
 939
 940        /*
 941         * Wait for this cmd to complete
 942         */
 943        wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
 944        cmd->sync_cmd = 0;
 945
 946        megasas_return_cmd(instance, cmd);
 947        return 0;
 948}
 949
 950/**
 951 * megasas_make_sgl32 - Prepares 32-bit SGL
 952 * @instance:           Adapter soft state
 953 * @scp:                SCSI command from the mid-layer
 954 * @mfi_sgl:            SGL to be filled in
 955 *
 956 * If successful, this function returns the number of SG elements. Otherwise,
 957 * it returnes -1.
 958 */
 959static int
 960megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
 961                   union megasas_sgl *mfi_sgl)
 962{
 963        int i;
 964        int sge_count;
 965        struct scatterlist *os_sgl;
 966
 967        sge_count = scsi_dma_map(scp);
 968        BUG_ON(sge_count < 0);
 969
 970        if (sge_count) {
 971                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
 972                        mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
 973                        mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
 974                }
 975        }
 976        return sge_count;
 977}
 978
 979/**
 980 * megasas_make_sgl64 - Prepares 64-bit SGL
 981 * @instance:           Adapter soft state
 982 * @scp:                SCSI command from the mid-layer
 983 * @mfi_sgl:            SGL to be filled in
 984 *
 985 * If successful, this function returns the number of SG elements. Otherwise,
 986 * it returnes -1.
 987 */
 988static int
 989megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
 990                   union megasas_sgl *mfi_sgl)
 991{
 992        int i;
 993        int sge_count;
 994        struct scatterlist *os_sgl;
 995
 996        sge_count = scsi_dma_map(scp);
 997        BUG_ON(sge_count < 0);
 998
 999        if (sge_count) {
1000                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1001                        mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1002                        mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1003                }
1004        }
1005        return sge_count;
1006}
1007
1008/**
1009 * megasas_make_sgl_skinny - Prepares IEEE SGL
1010 * @instance:           Adapter soft state
1011 * @scp:                SCSI command from the mid-layer
1012 * @mfi_sgl:            SGL to be filled in
1013 *
1014 * If successful, this function returns the number of SG elements. Otherwise,
1015 * it returnes -1.
1016 */
1017static int
1018megasas_make_sgl_skinny(struct megasas_instance *instance,
1019                struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1020{
1021        int i;
1022        int sge_count;
1023        struct scatterlist *os_sgl;
1024
1025        sge_count = scsi_dma_map(scp);
1026
1027        if (sge_count) {
1028                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1029                        mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1030                        mfi_sgl->sge_skinny[i].phys_addr =
1031                                                sg_dma_address(os_sgl);
1032                        mfi_sgl->sge_skinny[i].flag = 0;
1033                }
1034        }
1035        return sge_count;
1036}
1037
1038 /**
1039 * megasas_get_frame_count - Computes the number of frames
1040 * @frame_type          : type of frame- io or pthru frame
1041 * @sge_count           : number of sg elements
1042 *
1043 * Returns the number of frames required for numnber of sge's (sge_count)
1044 */
1045
1046static u32 megasas_get_frame_count(struct megasas_instance *instance,
1047                        u8 sge_count, u8 frame_type)
1048{
1049        int num_cnt;
1050        int sge_bytes;
1051        u32 sge_sz;
1052        u32 frame_count=0;
1053
1054        sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1055            sizeof(struct megasas_sge32);
1056
1057        if (instance->flag_ieee) {
1058                sge_sz = sizeof(struct megasas_sge_skinny);
1059        }
1060
1061        /*
1062         * Main frame can contain 2 SGEs for 64-bit SGLs and
1063         * 3 SGEs for 32-bit SGLs for ldio &
1064         * 1 SGEs for 64-bit SGLs and
1065         * 2 SGEs for 32-bit SGLs for pthru frame
1066         */
1067        if (unlikely(frame_type == PTHRU_FRAME)) {
1068                if (instance->flag_ieee == 1) {
1069                        num_cnt = sge_count - 1;
1070                } else if (IS_DMA64)
1071                        num_cnt = sge_count - 1;
1072                else
1073                        num_cnt = sge_count - 2;
1074        } else {
1075                if (instance->flag_ieee == 1) {
1076                        num_cnt = sge_count - 1;
1077                } else if (IS_DMA64)
1078                        num_cnt = sge_count - 2;
1079                else
1080                        num_cnt = sge_count - 3;
1081        }
1082
1083        if(num_cnt>0){
1084                sge_bytes = sge_sz * num_cnt;
1085
1086                frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1087                    ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1088        }
1089        /* Main frame */
1090        frame_count +=1;
1091
1092        if (frame_count > 7)
1093                frame_count = 8;
1094        return frame_count;
1095}
1096
1097/**
1098 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1099 * @instance:           Adapter soft state
1100 * @scp:                SCSI command
1101 * @cmd:                Command to be prepared in
1102 *
1103 * This function prepares CDB commands. These are typcially pass-through
1104 * commands to the devices.
1105 */
1106static int
1107megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1108                   struct megasas_cmd *cmd)
1109{
1110        u32 is_logical;
1111        u32 device_id;
1112        u16 flags = 0;
1113        struct megasas_pthru_frame *pthru;
1114
1115        is_logical = MEGASAS_IS_LOGICAL(scp);
1116        device_id = MEGASAS_DEV_INDEX(instance, scp);
1117        pthru = (struct megasas_pthru_frame *)cmd->frame;
1118
1119        if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1120                flags = MFI_FRAME_DIR_WRITE;
1121        else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1122                flags = MFI_FRAME_DIR_READ;
1123        else if (scp->sc_data_direction == PCI_DMA_NONE)
1124                flags = MFI_FRAME_DIR_NONE;
1125
1126        if (instance->flag_ieee == 1) {
1127                flags |= MFI_FRAME_IEEE;
1128        }
1129
1130        /*
1131         * Prepare the DCDB frame
1132         */
1133        pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1134        pthru->cmd_status = 0x0;
1135        pthru->scsi_status = 0x0;
1136        pthru->target_id = device_id;
1137        pthru->lun = scp->device->lun;
1138        pthru->cdb_len = scp->cmd_len;
1139        pthru->timeout = 0;
1140        pthru->pad_0 = 0;
1141        pthru->flags = flags;
1142        pthru->data_xfer_len = scsi_bufflen(scp);
1143
1144        memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1145
1146        /*
1147        * If the command is for the tape device, set the
1148        * pthru timeout to the os layer timeout value.
1149        */
1150        if (scp->device->type == TYPE_TAPE) {
1151                if ((scp->request->timeout / HZ) > 0xFFFF)
1152                        pthru->timeout = 0xFFFF;
1153                else
1154                        pthru->timeout = scp->request->timeout / HZ;
1155        }
1156
1157        /*
1158         * Construct SGL
1159         */
1160        if (instance->flag_ieee == 1) {
1161                pthru->flags |= MFI_FRAME_SGL64;
1162                pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1163                                                      &pthru->sgl);
1164        } else if (IS_DMA64) {
1165                pthru->flags |= MFI_FRAME_SGL64;
1166                pthru->sge_count = megasas_make_sgl64(instance, scp,
1167                                                      &pthru->sgl);
1168        } else
1169                pthru->sge_count = megasas_make_sgl32(instance, scp,
1170                                                      &pthru->sgl);
1171
1172        if (pthru->sge_count > instance->max_num_sge) {
1173                printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1174                        pthru->sge_count);
1175                return 0;
1176        }
1177
1178        /*
1179         * Sense info specific
1180         */
1181        pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1182        pthru->sense_buf_phys_addr_hi = 0;
1183        pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1184
1185        /*
1186         * Compute the total number of frames this command consumes. FW uses
1187         * this number to pull sufficient number of frames from host memory.
1188         */
1189        cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1190                                                        PTHRU_FRAME);
1191
1192        return cmd->frame_count;
1193}
1194
1195/**
1196 * megasas_build_ldio - Prepares IOs to logical devices
1197 * @instance:           Adapter soft state
1198 * @scp:                SCSI command
1199 * @cmd:                Command to be prepared
1200 *
1201 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1202 */
1203static int
1204megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1205                   struct megasas_cmd *cmd)
1206{
1207        u32 device_id;
1208        u8 sc = scp->cmnd[0];
1209        u16 flags = 0;
1210        struct megasas_io_frame *ldio;
1211
1212        device_id = MEGASAS_DEV_INDEX(instance, scp);
1213        ldio = (struct megasas_io_frame *)cmd->frame;
1214
1215        if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1216                flags = MFI_FRAME_DIR_WRITE;
1217        else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1218                flags = MFI_FRAME_DIR_READ;
1219
1220        if (instance->flag_ieee == 1) {
1221                flags |= MFI_FRAME_IEEE;
1222        }
1223
1224        /*
1225         * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1226         */
1227        ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1228        ldio->cmd_status = 0x0;
1229        ldio->scsi_status = 0x0;
1230        ldio->target_id = device_id;
1231        ldio->timeout = 0;
1232        ldio->reserved_0 = 0;
1233        ldio->pad_0 = 0;
1234        ldio->flags = flags;
1235        ldio->start_lba_hi = 0;
1236        ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1237
1238        /*
1239         * 6-byte READ(0x08) or WRITE(0x0A) cdb
1240         */
1241        if (scp->cmd_len == 6) {
1242                ldio->lba_count = (u32) scp->cmnd[4];
1243                ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1244                    ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1245
1246                ldio->start_lba_lo &= 0x1FFFFF;
1247        }
1248
1249        /*
1250         * 10-byte READ(0x28) or WRITE(0x2A) cdb
1251         */
1252        else if (scp->cmd_len == 10) {
1253                ldio->lba_count = (u32) scp->cmnd[8] |
1254                    ((u32) scp->cmnd[7] << 8);
1255                ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1256                    ((u32) scp->cmnd[3] << 16) |
1257                    ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1258        }
1259
1260        /*
1261         * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1262         */
1263        else if (scp->cmd_len == 12) {
1264                ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1265                    ((u32) scp->cmnd[7] << 16) |
1266                    ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1267
1268                ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1269                    ((u32) scp->cmnd[3] << 16) |
1270                    ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1271        }
1272
1273        /*
1274         * 16-byte READ(0x88) or WRITE(0x8A) cdb
1275         */
1276        else if (scp->cmd_len == 16) {
1277                ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1278                    ((u32) scp->cmnd[11] << 16) |
1279                    ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1280
1281                ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1282                    ((u32) scp->cmnd[7] << 16) |
1283                    ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1284
1285                ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1286                    ((u32) scp->cmnd[3] << 16) |
1287                    ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288
1289        }
1290
1291        /*
1292         * Construct SGL
1293         */
1294        if (instance->flag_ieee) {
1295                ldio->flags |= MFI_FRAME_SGL64;
1296                ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1297                                              &ldio->sgl);
1298        } else if (IS_DMA64) {
1299                ldio->flags |= MFI_FRAME_SGL64;
1300                ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1301        } else
1302                ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1303
1304        if (ldio->sge_count > instance->max_num_sge) {
1305                printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1306                        ldio->sge_count);
1307                return 0;
1308        }
1309
1310        /*
1311         * Sense info specific
1312         */
1313        ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1314        ldio->sense_buf_phys_addr_hi = 0;
1315        ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1316
1317        /*
1318         * Compute the total number of frames this command consumes. FW uses
1319         * this number to pull sufficient number of frames from host memory.
1320         */
1321        cmd->frame_count = megasas_get_frame_count(instance,
1322                        ldio->sge_count, IO_FRAME);
1323
1324        return cmd->frame_count;
1325}
1326
1327/**
1328 * megasas_is_ldio -            Checks if the cmd is for logical drive
1329 * @scmd:                       SCSI command
1330 *
1331 * Called by megasas_queue_command to find out if the command to be queued
1332 * is a logical drive command
1333 */
1334inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1335{
1336        if (!MEGASAS_IS_LOGICAL(cmd))
1337                return 0;
1338        switch (cmd->cmnd[0]) {
1339        case READ_10:
1340        case WRITE_10:
1341        case READ_12:
1342        case WRITE_12:
1343        case READ_6:
1344        case WRITE_6:
1345        case READ_16:
1346        case WRITE_16:
1347                return 1;
1348        default:
1349                return 0;
1350        }
1351}
1352
1353 /**
1354 * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1355 *                                      in FW
1356 * @instance:                           Adapter soft state
1357 */
1358static inline void
1359megasas_dump_pending_frames(struct megasas_instance *instance)
1360{
1361        struct megasas_cmd *cmd;
1362        int i,n;
1363        union megasas_sgl *mfi_sgl;
1364        struct megasas_io_frame *ldio;
1365        struct megasas_pthru_frame *pthru;
1366        u32 sgcount;
1367        u32 max_cmd = instance->max_fw_cmds;
1368
1369        printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1370        printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1371        if (IS_DMA64)
1372                printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1373        else
1374                printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1375
1376        printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1377        for (i = 0; i < max_cmd; i++) {
1378                cmd = instance->cmd_list[i];
1379                if(!cmd->scmd)
1380                        continue;
1381                printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1382                if (megasas_is_ldio(cmd->scmd)){
1383                        ldio = (struct megasas_io_frame *)cmd->frame;
1384                        mfi_sgl = &ldio->sgl;
1385                        sgcount = ldio->sge_count;
1386                        printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
1387                }
1388                else {
1389                        pthru = (struct megasas_pthru_frame *) cmd->frame;
1390                        mfi_sgl = &pthru->sgl;
1391                        sgcount = pthru->sge_count;
1392                        printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
1393                }
1394        if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1395                for (n = 0; n < sgcount; n++){
1396                        if (IS_DMA64)
1397                                printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
1398                        else
1399                                printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1400                        }
1401                }
1402                printk(KERN_ERR "\n");
1403        } /*for max_cmd*/
1404        printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1405        for (i = 0; i < max_cmd; i++) {
1406
1407                cmd = instance->cmd_list[i];
1408
1409                if(cmd->sync_cmd == 1){
1410                        printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1411                }
1412        }
1413        printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1414}
1415
1416u32
1417megasas_build_and_issue_cmd(struct megasas_instance *instance,
1418                            struct scsi_cmnd *scmd)
1419{
1420        struct megasas_cmd *cmd;
1421        u32 frame_count;
1422
1423        cmd = megasas_get_cmd(instance);
1424        if (!cmd)
1425                return SCSI_MLQUEUE_HOST_BUSY;
1426
1427        /*
1428         * Logical drive command
1429         */
1430        if (megasas_is_ldio(scmd))
1431                frame_count = megasas_build_ldio(instance, scmd, cmd);
1432        else
1433                frame_count = megasas_build_dcdb(instance, scmd, cmd);
1434
1435        if (!frame_count)
1436                goto out_return_cmd;
1437
1438        cmd->scmd = scmd;
1439        scmd->SCp.ptr = (char *)cmd;
1440
1441        /*
1442         * Issue the command to the FW
1443         */
1444        atomic_inc(&instance->fw_outstanding);
1445
1446        instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1447                                cmd->frame_count-1, instance->reg_set);
1448
1449        return 0;
1450out_return_cmd:
1451        megasas_return_cmd(instance, cmd);
1452        return 1;
1453}
1454
1455
1456/**
1457 * megasas_queue_command -      Queue entry point
1458 * @scmd:                       SCSI command to be queued
1459 * @done:                       Callback entry point
1460 */
1461static int
1462megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1463{
1464        struct megasas_instance *instance;
1465        unsigned long flags;
1466
1467        instance = (struct megasas_instance *)
1468            scmd->device->host->hostdata;
1469
1470        if (instance->issuepend_done == 0)
1471                return SCSI_MLQUEUE_HOST_BUSY;
1472
1473        spin_lock_irqsave(&instance->hba_lock, flags);
1474        if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1475                spin_unlock_irqrestore(&instance->hba_lock, flags);
1476                return SCSI_MLQUEUE_HOST_BUSY;
1477        }
1478
1479        spin_unlock_irqrestore(&instance->hba_lock, flags);
1480
1481        scmd->scsi_done = done;
1482        scmd->result = 0;
1483
1484        if (MEGASAS_IS_LOGICAL(scmd) &&
1485            (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1486                scmd->result = DID_BAD_TARGET << 16;
1487                goto out_done;
1488        }
1489
1490        switch (scmd->cmnd[0]) {
1491        case SYNCHRONIZE_CACHE:
1492                /*
1493                 * FW takes care of flush cache on its own
1494                 * No need to send it down
1495                 */
1496                scmd->result = DID_OK << 16;
1497                goto out_done;
1498        default:
1499                break;
1500        }
1501
1502        if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1503                printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1504                return SCSI_MLQUEUE_HOST_BUSY;
1505        }
1506
1507        return 0;
1508
1509 out_done:
1510        done(scmd);
1511        return 0;
1512}
1513
1514static DEF_SCSI_QCMD(megasas_queue_command)
1515
1516static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1517{
1518        int i;
1519
1520        for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1521
1522                if ((megasas_mgmt_info.instance[i]) &&
1523                    (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1524                        return megasas_mgmt_info.instance[i];
1525        }
1526
1527        return NULL;
1528}
1529
1530static int megasas_slave_configure(struct scsi_device *sdev)
1531{
1532        u16             pd_index = 0;
1533        struct  megasas_instance *instance ;
1534
1535        instance = megasas_lookup_instance(sdev->host->host_no);
1536
1537        /*
1538        * Don't export physical disk devices to the disk driver.
1539        *
1540        * FIXME: Currently we don't export them to the midlayer at all.
1541        *        That will be fixed once LSI engineers have audited the
1542        *        firmware for possible issues.
1543        */
1544        if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1545                                sdev->type == TYPE_DISK) {
1546                pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1547                                                                sdev->id;
1548                if (instance->pd_list[pd_index].driveState ==
1549                                                MR_PD_STATE_SYSTEM) {
1550                        blk_queue_rq_timeout(sdev->request_queue,
1551                                MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1552                        return 0;
1553                }
1554                return -ENXIO;
1555        }
1556
1557        /*
1558        * The RAID firmware may require extended timeouts.
1559        */
1560        blk_queue_rq_timeout(sdev->request_queue,
1561                MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1562        return 0;
1563}
1564
1565static int megasas_slave_alloc(struct scsi_device *sdev)
1566{
1567        u16             pd_index = 0;
1568        struct megasas_instance *instance ;
1569        instance = megasas_lookup_instance(sdev->host->host_no);
1570        if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1571                                (sdev->type == TYPE_DISK)) {
1572                /*
1573                 * Open the OS scan to the SYSTEM PD
1574                 */
1575                pd_index =
1576                        (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1577                        sdev->id;
1578                if ((instance->pd_list[pd_index].driveState ==
1579                                        MR_PD_STATE_SYSTEM) &&
1580                        (instance->pd_list[pd_index].driveType ==
1581                                                TYPE_DISK)) {
1582                        return 0;
1583                }
1584                return -ENXIO;
1585        }
1586        return 0;
1587}
1588
1589void megaraid_sas_kill_hba(struct megasas_instance *instance)
1590{
1591        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1592            (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1593            (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1594            (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
1595                writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1596        } else {
1597                writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1598        }
1599}
1600
1601 /**
1602  * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1603  *                                     restored to max value
1604  * @instance:                  Adapter soft state
1605  *
1606  */
1607void
1608megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1609{
1610        unsigned long flags;
1611        if (instance->flag & MEGASAS_FW_BUSY
1612            && time_after(jiffies, instance->last_time + 5 * HZ)
1613            && atomic_read(&instance->fw_outstanding) <
1614            instance->throttlequeuedepth + 1) {
1615
1616                spin_lock_irqsave(instance->host->host_lock, flags);
1617                instance->flag &= ~MEGASAS_FW_BUSY;
1618                if ((instance->pdev->device ==
1619                        PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1620                        (instance->pdev->device ==
1621                        PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1622                        instance->host->can_queue =
1623                                instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1624                } else
1625                        instance->host->can_queue =
1626                                instance->max_fw_cmds - MEGASAS_INT_CMDS;
1627
1628                spin_unlock_irqrestore(instance->host->host_lock, flags);
1629        }
1630}
1631
1632/**
1633 * megasas_complete_cmd_dpc      -      Returns FW's controller structure
1634 * @instance_addr:                      Address of adapter soft state
1635 *
1636 * Tasklet to complete cmds
1637 */
1638static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1639{
1640        u32 producer;
1641        u32 consumer;
1642        u32 context;
1643        struct megasas_cmd *cmd;
1644        struct megasas_instance *instance =
1645                                (struct megasas_instance *)instance_addr;
1646        unsigned long flags;
1647
1648        /* If we have already declared adapter dead, donot complete cmds */
1649        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1650                return;
1651
1652        spin_lock_irqsave(&instance->completion_lock, flags);
1653
1654        producer = *instance->producer;
1655        consumer = *instance->consumer;
1656
1657        while (consumer != producer) {
1658                context = instance->reply_queue[consumer];
1659                if (context >= instance->max_fw_cmds) {
1660                        printk(KERN_ERR "Unexpected context value %x\n",
1661                                context);
1662                        BUG();
1663                }
1664
1665                cmd = instance->cmd_list[context];
1666
1667                megasas_complete_cmd(instance, cmd, DID_OK);
1668
1669                consumer++;
1670                if (consumer == (instance->max_fw_cmds + 1)) {
1671                        consumer = 0;
1672                }
1673        }
1674
1675        *instance->consumer = producer;
1676
1677        spin_unlock_irqrestore(&instance->completion_lock, flags);
1678
1679        /*
1680         * Check if we can restore can_queue
1681         */
1682        megasas_check_and_restore_queue_depth(instance);
1683}
1684
1685static void
1686megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687
1688static void
1689process_fw_state_change_wq(struct work_struct *work);
1690
1691void megasas_do_ocr(struct megasas_instance *instance)
1692{
1693        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1694        (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1695        (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1696                *instance->consumer     = MEGASAS_ADPRESET_INPROG_SIGN;
1697        }
1698        instance->instancet->disable_intr(instance->reg_set);
1699        instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
1700        instance->issuepend_done = 0;
1701
1702        atomic_set(&instance->fw_outstanding, 0);
1703        megasas_internal_reset_defer_cmds(instance);
1704        process_fw_state_change_wq(&instance->work_init);
1705}
1706
1707/**
1708 * megasas_wait_for_outstanding -       Wait for all outstanding cmds
1709 * @instance:                           Adapter soft state
1710 *
1711 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1712 * complete all its outstanding commands. Returns error if one or more IOs
1713 * are pending after this time period. It also marks the controller dead.
1714 */
1715static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1716{
1717        int i;
1718        u32 reset_index;
1719        u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1720        u8 adprecovery;
1721        unsigned long flags;
1722        struct list_head clist_local;
1723        struct megasas_cmd *reset_cmd;
1724        u32 fw_state;
1725        u8 kill_adapter_flag;
1726
1727        spin_lock_irqsave(&instance->hba_lock, flags);
1728        adprecovery = instance->adprecovery;
1729        spin_unlock_irqrestore(&instance->hba_lock, flags);
1730
1731        if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1732
1733                INIT_LIST_HEAD(&clist_local);
1734                spin_lock_irqsave(&instance->hba_lock, flags);
1735                list_splice_init(&instance->internal_reset_pending_q,
1736                                &clist_local);
1737                spin_unlock_irqrestore(&instance->hba_lock, flags);
1738
1739                printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1740                for (i = 0; i < wait_time; i++) {
1741                        msleep(1000);
1742                        spin_lock_irqsave(&instance->hba_lock, flags);
1743                        adprecovery = instance->adprecovery;
1744                        spin_unlock_irqrestore(&instance->hba_lock, flags);
1745                        if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1746                                break;
1747                }
1748
1749                if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1750                        printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1751                        spin_lock_irqsave(&instance->hba_lock, flags);
1752                        instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
1753                        spin_unlock_irqrestore(&instance->hba_lock, flags);
1754                        return FAILED;
1755                }
1756
1757                reset_index     = 0;
1758                while (!list_empty(&clist_local)) {
1759                        reset_cmd       = list_entry((&clist_local)->next,
1760                                                struct megasas_cmd, list);
1761                        list_del_init(&reset_cmd->list);
1762                        if (reset_cmd->scmd) {
1763                                reset_cmd->scmd->result = DID_RESET << 16;
1764                                printk(KERN_NOTICE "%d:%p reset [%02x]\n",
1765                                        reset_index, reset_cmd,
1766                                        reset_cmd->scmd->cmnd[0]);
1767
1768                                reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1769                                megasas_return_cmd(instance, reset_cmd);
1770                        } else if (reset_cmd->sync_cmd) {
1771                                printk(KERN_NOTICE "megasas:%p synch cmds"
1772                                                "reset queue\n",
1773                                                reset_cmd);
1774
1775                                reset_cmd->cmd_status = ENODATA;
1776                                instance->instancet->fire_cmd(instance,
1777                                                reset_cmd->frame_phys_addr,
1778                                                0, instance->reg_set);
1779                        } else {
1780                                printk(KERN_NOTICE "megasas: %p unexpected"
1781                                        "cmds lst\n",
1782                                        reset_cmd);
1783                        }
1784                        reset_index++;
1785                }
1786
1787                return SUCCESS;
1788        }
1789
1790        for (i = 0; i < resetwaittime; i++) {
1791
1792                int outstanding = atomic_read(&instance->fw_outstanding);
1793
1794                if (!outstanding)
1795                        break;
1796
1797                if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1798                        printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1799                               "commands to complete\n",i,outstanding);
1800                        /*
1801                         * Call cmd completion routine. Cmd to be
1802                         * be completed directly without depending on isr.
1803                         */
1804                        megasas_complete_cmd_dpc((unsigned long)instance);
1805                }
1806
1807                msleep(1000);
1808        }
1809
1810        i = 0;
1811        kill_adapter_flag = 0;
1812        do {
1813                fw_state = instance->instancet->read_fw_status_reg(
1814                                        instance->reg_set) & MFI_STATE_MASK;
1815                if ((fw_state == MFI_STATE_FAULT) &&
1816                        (instance->disableOnlineCtrlReset == 0)) {
1817                        if (i == 3) {
1818                                kill_adapter_flag = 2;
1819                                break;
1820                        }
1821                        megasas_do_ocr(instance);
1822                        kill_adapter_flag = 1;
1823
1824                        /* wait for 1 secs to let FW finish the pending cmds */
1825                        msleep(1000);
1826                }
1827                i++;
1828        } while (i <= 3);
1829
1830        if (atomic_read(&instance->fw_outstanding) &&
1831                                        !kill_adapter_flag) {
1832                if (instance->disableOnlineCtrlReset == 0) {
1833
1834                        megasas_do_ocr(instance);
1835
1836                        /* wait for 5 secs to let FW finish the pending cmds */
1837                        for (i = 0; i < wait_time; i++) {
1838                                int outstanding =
1839                                        atomic_read(&instance->fw_outstanding);
1840                                if (!outstanding)
1841                                        return SUCCESS;
1842                                msleep(1000);
1843                        }
1844                }
1845        }
1846
1847        if (atomic_read(&instance->fw_outstanding) ||
1848                                        (kill_adapter_flag == 2)) {
1849                printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1850                /*
1851                * Send signal to FW to stop processing any pending cmds.
1852                * The controller will be taken offline by the OS now.
1853                */
1854                if ((instance->pdev->device ==
1855                        PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1856                        (instance->pdev->device ==
1857                        PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1858                        writel(MFI_STOP_ADP,
1859                                &instance->reg_set->doorbell);
1860                } else {
1861                        writel(MFI_STOP_ADP,
1862                                &instance->reg_set->inbound_doorbell);
1863                }
1864                megasas_dump_pending_frames(instance);
1865                spin_lock_irqsave(&instance->hba_lock, flags);
1866                instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
1867                spin_unlock_irqrestore(&instance->hba_lock, flags);
1868                return FAILED;
1869        }
1870
1871        printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1872
1873        return SUCCESS;
1874}
1875
1876/**
1877 * megasas_generic_reset -      Generic reset routine
1878 * @scmd:                       Mid-layer SCSI command
1879 *
1880 * This routine implements a generic reset handler for device, bus and host
1881 * reset requests. Device, bus and host specific reset handlers can use this
1882 * function after they do their specific tasks.
1883 */
1884static int megasas_generic_reset(struct scsi_cmnd *scmd)
1885{
1886        int ret_val;
1887        struct megasas_instance *instance;
1888
1889        instance = (struct megasas_instance *)scmd->device->host->hostdata;
1890
1891        scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1892                 scmd->cmnd[0], scmd->retries);
1893
1894        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1895                printk(KERN_ERR "megasas: cannot recover from previous reset "
1896                       "failures\n");
1897                return FAILED;
1898        }
1899
1900        ret_val = megasas_wait_for_outstanding(instance);
1901        if (ret_val == SUCCESS)
1902                printk(KERN_NOTICE "megasas: reset successful \n");
1903        else
1904                printk(KERN_ERR "megasas: failed to do reset\n");
1905
1906        return ret_val;
1907}
1908
1909/**
1910 * megasas_reset_timer - quiesce the adapter if required
1911 * @scmd:               scsi cmnd
1912 *
1913 * Sets the FW busy flag and reduces the host->can_queue if the
1914 * cmd has not been completed within the timeout period.
1915 */
1916static enum
1917blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1918{
1919        struct megasas_instance *instance;
1920        unsigned long flags;
1921
1922        if (time_after(jiffies, scmd->jiffies_at_alloc +
1923                                (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1924                return BLK_EH_NOT_HANDLED;
1925        }
1926
1927        instance = (struct megasas_instance *)scmd->device->host->hostdata;
1928        if (!(instance->flag & MEGASAS_FW_BUSY)) {
1929                /* FW is busy, throttle IO */
1930                spin_lock_irqsave(instance->host->host_lock, flags);
1931
1932                instance->host->can_queue = instance->throttlequeuedepth;
1933                instance->last_time = jiffies;
1934                instance->flag |= MEGASAS_FW_BUSY;
1935
1936                spin_unlock_irqrestore(instance->host->host_lock, flags);
1937        }
1938        return BLK_EH_RESET_TIMER;
1939}
1940
1941/**
1942 * megasas_reset_device -       Device reset handler entry point
1943 */
1944static int megasas_reset_device(struct scsi_cmnd *scmd)
1945{
1946        int ret;
1947
1948        /*
1949         * First wait for all commands to complete
1950         */
1951        ret = megasas_generic_reset(scmd);
1952
1953        return ret;
1954}
1955
1956/**
1957 * megasas_reset_bus_host -     Bus & host reset handler entry point
1958 */
1959static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1960{
1961        int ret;
1962        struct megasas_instance *instance;
1963        instance = (struct megasas_instance *)scmd->device->host->hostdata;
1964
1965        /*
1966         * First wait for all commands to complete
1967         */
1968        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1969            (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
1970                ret = megasas_reset_fusion(scmd->device->host);
1971        else
1972                ret = megasas_generic_reset(scmd);
1973
1974        return ret;
1975}
1976
1977/**
1978 * megasas_bios_param - Returns disk geometry for a disk
1979 * @sdev:               device handle
1980 * @bdev:               block device
1981 * @capacity:           drive capacity
1982 * @geom:               geometry parameters
1983 */
1984static int
1985megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1986                 sector_t capacity, int geom[])
1987{
1988        int heads;
1989        int sectors;
1990        sector_t cylinders;
1991        unsigned long tmp;
1992        /* Default heads (64) & sectors (32) */
1993        heads = 64;
1994        sectors = 32;
1995
1996        tmp = heads * sectors;
1997        cylinders = capacity;
1998
1999        sector_div(cylinders, tmp);
2000
2001        /*
2002         * Handle extended translation size for logical drives > 1Gb
2003         */
2004
2005        if (capacity >= 0x200000) {
2006                heads = 255;
2007                sectors = 63;
2008                tmp = heads*sectors;
2009                cylinders = capacity;
2010                sector_div(cylinders, tmp);
2011        }
2012
2013        geom[0] = heads;
2014        geom[1] = sectors;
2015        geom[2] = cylinders;
2016
2017        return 0;
2018}
2019
2020static void megasas_aen_polling(struct work_struct *work);
2021
2022/**
2023 * megasas_service_aen -        Processes an event notification
2024 * @instance:                   Adapter soft state
2025 * @cmd:                        AEN command completed by the ISR
2026 *
2027 * For AEN, driver sends a command down to FW that is held by the FW till an
2028 * event occurs. When an event of interest occurs, FW completes the command
2029 * that it was previously holding.
2030 *
2031 * This routines sends SIGIO signal to processes that have registered with the
2032 * driver for AEN.
2033 */
2034static void
2035megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2036{
2037        unsigned long flags;
2038        /*
2039         * Don't signal app if it is just an aborted previously registered aen
2040         */
2041        if ((!cmd->abort_aen) && (instance->unload == 0)) {
2042                spin_lock_irqsave(&poll_aen_lock, flags);
2043                megasas_poll_wait_aen = 1;
2044                spin_unlock_irqrestore(&poll_aen_lock, flags);
2045                wake_up(&megasas_poll_wait);
2046                kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2047        }
2048        else
2049                cmd->abort_aen = 0;
2050
2051        instance->aen_cmd = NULL;
2052        megasas_return_cmd(instance, cmd);
2053
2054        if ((instance->unload == 0) &&
2055                ((instance->issuepend_done == 1))) {
2056                struct megasas_aen_event *ev;
2057                ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2058                if (!ev) {
2059                        printk(KERN_ERR "megasas_service_aen: out of memory\n");
2060                } else {
2061                        ev->instance = instance;
2062                        instance->ev = ev;
2063                        INIT_DELAYED_WORK(&ev->hotplug_work,
2064                                          megasas_aen_polling);
2065                        schedule_delayed_work(&ev->hotplug_work, 0);
2066                }
2067        }
2068}
2069
2070static int megasas_change_queue_depth(struct scsi_device *sdev,
2071                                      int queue_depth, int reason)
2072{
2073        if (reason != SCSI_QDEPTH_DEFAULT)
2074                return -EOPNOTSUPP;
2075
2076        if (queue_depth > sdev->host->can_queue)
2077                queue_depth = sdev->host->can_queue;
2078        scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2079                                queue_depth);
2080
2081        return queue_depth;
2082}
2083
2084/*
2085 * Scsi host template for megaraid_sas driver
2086 */
2087static struct scsi_host_template megasas_template = {
2088
2089        .module = THIS_MODULE,
2090        .name = "LSI SAS based MegaRAID driver",
2091        .proc_name = "megaraid_sas",
2092        .slave_configure = megasas_slave_configure,
2093        .slave_alloc = megasas_slave_alloc,
2094        .queuecommand = megasas_queue_command,
2095        .eh_device_reset_handler = megasas_reset_device,
2096        .eh_bus_reset_handler = megasas_reset_bus_host,
2097        .eh_host_reset_handler = megasas_reset_bus_host,
2098        .eh_timed_out = megasas_reset_timer,
2099        .bios_param = megasas_bios_param,
2100        .use_clustering = ENABLE_CLUSTERING,
2101        .change_queue_depth = megasas_change_queue_depth,
2102};
2103
2104/**
2105 * megasas_complete_int_cmd -   Completes an internal command
2106 * @instance:                   Adapter soft state
2107 * @cmd:                        Command to be completed
2108 *
2109 * The megasas_issue_blocked_cmd() function waits for a command to complete
2110 * after it issues a command. This function wakes up that waiting routine by
2111 * calling wake_up() on the wait queue.
2112 */
2113static void
2114megasas_complete_int_cmd(struct megasas_instance *instance,
2115                         struct megasas_cmd *cmd)
2116{
2117        cmd->cmd_status = cmd->frame->io.cmd_status;
2118
2119        if (cmd->cmd_status == ENODATA) {
2120                cmd->cmd_status = 0;
2121        }
2122        wake_up(&instance->int_cmd_wait_q);
2123}
2124
2125/**
2126 * megasas_complete_abort -     Completes aborting a command
2127 * @instance:                   Adapter soft state
2128 * @cmd:                        Cmd that was issued to abort another cmd
2129 *
2130 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2131 * after it issues an abort on a previously issued command. This function
2132 * wakes up all functions waiting on the same wait queue.
2133 */
2134static void
2135megasas_complete_abort(struct megasas_instance *instance,
2136                       struct megasas_cmd *cmd)
2137{
2138        if (cmd->sync_cmd) {
2139                cmd->sync_cmd = 0;
2140                cmd->cmd_status = 0;
2141                wake_up(&instance->abort_cmd_wait_q);
2142        }
2143
2144        return;
2145}
2146
2147/**
2148 * megasas_complete_cmd -       Completes a command
2149 * @instance:                   Adapter soft state
2150 * @cmd:                        Command to be completed
2151 * @alt_status:                 If non-zero, use this value as status to
2152 *                              SCSI mid-layer instead of the value returned
2153 *                              by the FW. This should be used if caller wants
2154 *                              an alternate status (as in the case of aborted
2155 *                              commands)
2156 */
2157void
2158megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2159                     u8 alt_status)
2160{
2161        int exception = 0;
2162        struct megasas_header *hdr = &cmd->frame->hdr;
2163        unsigned long flags;
2164        struct fusion_context *fusion = instance->ctrl_context;
2165
2166        /* flag for the retry reset */
2167        cmd->retry_for_fw_reset = 0;
2168
2169        if (cmd->scmd)
2170                cmd->scmd->SCp.ptr = NULL;
2171
2172        switch (hdr->cmd) {
2173        case MFI_CMD_INVALID:
2174                /* Some older 1068 controller FW may keep a pended
2175                   MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2176                   when booting the kdump kernel.  Ignore this command to
2177                   prevent a kernel panic on shutdown of the kdump kernel. */
2178                printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2179                       "completed.\n");
2180                printk(KERN_WARNING "megaraid_sas: If you have a controller "
2181                       "other than PERC5, please upgrade your firmware.\n");
2182                break;
2183        case MFI_CMD_PD_SCSI_IO:
2184        case MFI_CMD_LD_SCSI_IO:
2185
2186                /*
2187                 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2188                 * issued either through an IO path or an IOCTL path. If it
2189                 * was via IOCTL, we will send it to internal completion.
2190                 */
2191                if (cmd->sync_cmd) {
2192                        cmd->sync_cmd = 0;
2193                        megasas_complete_int_cmd(instance, cmd);
2194                        break;
2195                }
2196
2197        case MFI_CMD_LD_READ:
2198        case MFI_CMD_LD_WRITE:
2199
2200                if (alt_status) {
2201                        cmd->scmd->result = alt_status << 16;
2202                        exception = 1;
2203                }
2204
2205                if (exception) {
2206
2207                        atomic_dec(&instance->fw_outstanding);
2208
2209                        scsi_dma_unmap(cmd->scmd);
2210                        cmd->scmd->scsi_done(cmd->scmd);
2211                        megasas_return_cmd(instance, cmd);
2212
2213                        break;
2214                }
2215
2216                switch (hdr->cmd_status) {
2217
2218                case MFI_STAT_OK:
2219                        cmd->scmd->result = DID_OK << 16;
2220                        break;
2221
2222                case MFI_STAT_SCSI_IO_FAILED:
2223                case MFI_STAT_LD_INIT_IN_PROGRESS:
2224                        cmd->scmd->result =
2225                            (DID_ERROR << 16) | hdr->scsi_status;
2226                        break;
2227
2228                case MFI_STAT_SCSI_DONE_WITH_ERROR:
2229
2230                        cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2231
2232                        if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2233                                memset(cmd->scmd->sense_buffer, 0,
2234                                       SCSI_SENSE_BUFFERSIZE);
2235                                memcpy(cmd->scmd->sense_buffer, cmd->sense,
2236                                       hdr->sense_len);
2237
2238                                cmd->scmd->result |= DRIVER_SENSE << 24;
2239                        }
2240
2241                        break;
2242
2243                case MFI_STAT_LD_OFFLINE:
2244                case MFI_STAT_DEVICE_NOT_FOUND:
2245                        cmd->scmd->result = DID_BAD_TARGET << 16;
2246                        break;
2247
2248                default:
2249                        printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2250                               hdr->cmd_status);
2251                        cmd->scmd->result = DID_ERROR << 16;
2252                        break;
2253                }
2254
2255                atomic_dec(&instance->fw_outstanding);
2256
2257                scsi_dma_unmap(cmd->scmd);
2258                cmd->scmd->scsi_done(cmd->scmd);
2259                megasas_return_cmd(instance, cmd);
2260
2261                break;
2262
2263        case MFI_CMD_SMP:
2264        case MFI_CMD_STP:
2265        case MFI_CMD_DCMD:
2266                /* Check for LD map update */
2267                if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2268                    (cmd->frame->dcmd.mbox.b[1] == 1)) {
2269                        spin_lock_irqsave(instance->host->host_lock, flags);
2270                        if (cmd->frame->hdr.cmd_status != 0) {
2271                                if (cmd->frame->hdr.cmd_status !=
2272                                    MFI_STAT_NOT_FOUND)
2273                                        printk(KERN_WARNING "megasas: map sync"
2274                                               "failed, status = 0x%x.\n",
2275                                               cmd->frame->hdr.cmd_status);
2276                                else {
2277                                        megasas_return_cmd(instance, cmd);
2278                                        spin_unlock_irqrestore(
2279                                                instance->host->host_lock,
2280                                                flags);
2281                                        break;
2282                                }
2283                        } else
2284                                instance->map_id++;
2285                        megasas_return_cmd(instance, cmd);
2286                        if (MR_ValidateMapInfo(
2287                                    fusion->ld_map[(instance->map_id & 1)],
2288                                    fusion->load_balance_info))
2289                                fusion->fast_path_io = 1;
2290                        else
2291                                fusion->fast_path_io = 0;
2292                        megasas_sync_map_info(instance);
2293                        spin_unlock_irqrestore(instance->host->host_lock,
2294                                               flags);
2295                        break;
2296                }
2297                if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2298                        cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2299                        spin_lock_irqsave(&poll_aen_lock, flags);
2300                        megasas_poll_wait_aen = 0;
2301                        spin_unlock_irqrestore(&poll_aen_lock, flags);
2302                }
2303
2304                /*
2305                 * See if got an event notification
2306                 */
2307                if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2308                        megasas_service_aen(instance, cmd);
2309                else
2310                        megasas_complete_int_cmd(instance, cmd);
2311
2312                break;
2313
2314        case MFI_CMD_ABORT:
2315                /*
2316                 * Cmd issued to abort another cmd returned
2317                 */
2318                megasas_complete_abort(instance, cmd);
2319                break;
2320
2321        default:
2322                printk("megasas: Unknown command completed! [0x%X]\n",
2323                       hdr->cmd);
2324                break;
2325        }
2326}
2327
2328/**
2329 * megasas_issue_pending_cmds_again -   issue all pending cmds
2330 *                                      in FW again because of the fw reset
2331 * @instance:                           Adapter soft state
2332 */
2333static inline void
2334megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2335{
2336        struct megasas_cmd *cmd;
2337        struct list_head clist_local;
2338        union megasas_evt_class_locale class_locale;
2339        unsigned long flags;
2340        u32 seq_num;
2341
2342        INIT_LIST_HEAD(&clist_local);
2343        spin_lock_irqsave(&instance->hba_lock, flags);
2344        list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2345        spin_unlock_irqrestore(&instance->hba_lock, flags);
2346
2347        while (!list_empty(&clist_local)) {
2348                cmd     = list_entry((&clist_local)->next,
2349                                        struct megasas_cmd, list);
2350                list_del_init(&cmd->list);
2351
2352                if (cmd->sync_cmd || cmd->scmd) {
2353                        printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2354                                "detected to be pending while HBA reset.\n",
2355                                        cmd, cmd->scmd, cmd->sync_cmd);
2356
2357                        cmd->retry_for_fw_reset++;
2358
2359                        if (cmd->retry_for_fw_reset == 3) {
2360                                printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2361                                        "was tried multiple times during reset."
2362                                        "Shutting down the HBA\n",
2363                                        cmd, cmd->scmd, cmd->sync_cmd);
2364                                megaraid_sas_kill_hba(instance);
2365
2366                                instance->adprecovery =
2367                                                MEGASAS_HW_CRITICAL_ERROR;
2368                                return;
2369                        }
2370                }
2371
2372                if (cmd->sync_cmd == 1) {
2373                        if (cmd->scmd) {
2374                                printk(KERN_NOTICE "megaraid_sas: unexpected"
2375                                        "cmd attached to internal command!\n");
2376                        }
2377                        printk(KERN_NOTICE "megasas: %p synchronous cmd"
2378                                                "on the internal reset queue,"
2379                                                "issue it again.\n", cmd);
2380                        cmd->cmd_status = ENODATA;
2381                        instance->instancet->fire_cmd(instance,
2382                                                        cmd->frame_phys_addr ,
2383                                                        0, instance->reg_set);
2384                } else if (cmd->scmd) {
2385                        printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
2386                        "detected on the internal queue, issue again.\n",
2387                        cmd, cmd->scmd->cmnd[0]);
2388
2389                        atomic_inc(&instance->fw_outstanding);
2390                        instance->instancet->fire_cmd(instance,
2391                                        cmd->frame_phys_addr,
2392                                        cmd->frame_count-1, instance->reg_set);
2393                } else {
2394                        printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2395                                "internal reset defer list while re-issue!!\n",
2396                                cmd);
2397                }
2398        }
2399
2400        if (instance->aen_cmd) {
2401                printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2402                megasas_return_cmd(instance, instance->aen_cmd);
2403
2404                instance->aen_cmd       = NULL;
2405        }
2406
2407        /*
2408        * Initiate AEN (Asynchronous Event Notification)
2409        */
2410        seq_num = instance->last_seq_num;
2411        class_locale.members.reserved = 0;
2412        class_locale.members.locale = MR_EVT_LOCALE_ALL;
2413        class_locale.members.class = MR_EVT_CLASS_DEBUG;
2414
2415        megasas_register_aen(instance, seq_num, class_locale.word);
2416}
2417
2418/**
2419 * Move the internal reset pending commands to a deferred queue.
2420 *
2421 * We move the commands pending at internal reset time to a
2422 * pending queue. This queue would be flushed after successful
2423 * completion of the internal reset sequence. if the internal reset
2424 * did not complete in time, the kernel reset handler would flush
2425 * these commands.
2426 **/
2427static void
2428megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2429{
2430        struct megasas_cmd *cmd;
2431        int i;
2432        u32 max_cmd = instance->max_fw_cmds;
2433        u32 defer_index;
2434        unsigned long flags;
2435
2436        defer_index     = 0;
2437        spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2438        for (i = 0; i < max_cmd; i++) {
2439                cmd = instance->cmd_list[i];
2440                if (cmd->sync_cmd == 1 || cmd->scmd) {
2441                        printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2442                                        "on the defer queue as internal\n",
2443                                defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2444
2445                        if (!list_empty(&cmd->list)) {
2446                                printk(KERN_NOTICE "megaraid_sas: ERROR while"
2447                                        " moving this cmd:%p, %d %p, it was"
2448                                        "discovered on some list?\n",
2449                                        cmd, cmd->sync_cmd, cmd->scmd);
2450
2451                                list_del_init(&cmd->list);
2452                        }
2453                        defer_index++;
2454                        list_add_tail(&cmd->list,
2455                                &instance->internal_reset_pending_q);
2456                }
2457        }
2458        spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2459}
2460
2461
2462static void
2463process_fw_state_change_wq(struct work_struct *work)
2464{
2465        struct megasas_instance *instance =
2466                container_of(work, struct megasas_instance, work_init);
2467        u32 wait;
2468        unsigned long flags;
2469
2470        if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2471                printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2472                                instance->adprecovery);
2473                return ;
2474        }
2475
2476        if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2477                printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2478                                        "state, restarting it...\n");
2479
2480                instance->instancet->disable_intr(instance->reg_set);
2481                atomic_set(&instance->fw_outstanding, 0);
2482
2483                atomic_set(&instance->fw_reset_no_pci_access, 1);
2484                instance->instancet->adp_reset(instance, instance->reg_set);
2485                atomic_set(&instance->fw_reset_no_pci_access, 0 );
2486
2487                printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2488                                        "initiating next stage...\n");
2489
2490                printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2491                                        "state 2 starting...\n");
2492
2493                /*waitting for about 20 second before start the second init*/
2494                for (wait = 0; wait < 30; wait++) {
2495                        msleep(1000);
2496                }
2497
2498                if (megasas_transition_to_ready(instance, 1)) {
2499                        printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2500
2501                        megaraid_sas_kill_hba(instance);
2502                        instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
2503                        return ;
2504                }
2505
2506                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2507                        (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2508                        (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2509                        ) {
2510                        *instance->consumer = *instance->producer;
2511                } else {
2512                        *instance->consumer = 0;
2513                        *instance->producer = 0;
2514                }
2515
2516                megasas_issue_init_mfi(instance);
2517
2518                spin_lock_irqsave(&instance->hba_lock, flags);
2519                instance->adprecovery   = MEGASAS_HBA_OPERATIONAL;
2520                spin_unlock_irqrestore(&instance->hba_lock, flags);
2521                instance->instancet->enable_intr(instance->reg_set);
2522
2523                megasas_issue_pending_cmds_again(instance);
2524                instance->issuepend_done = 1;
2525        }
2526        return ;
2527}
2528
2529/**
2530 * megasas_deplete_reply_queue -        Processes all completed commands
2531 * @instance:                           Adapter soft state
2532 * @alt_status:                         Alternate status to be returned to
2533 *                                      SCSI mid-layer instead of the status
2534 *                                      returned by the FW
2535 * Note: this must be called with hba lock held
2536 */
2537static int
2538megasas_deplete_reply_queue(struct megasas_instance *instance,
2539                                        u8 alt_status)
2540{
2541        u32 mfiStatus;
2542        u32 fw_state;
2543
2544        if ((mfiStatus = instance->instancet->check_reset(instance,
2545                                        instance->reg_set)) == 1) {
2546                return IRQ_HANDLED;
2547        }
2548
2549        if ((mfiStatus = instance->instancet->clear_intr(
2550                                                instance->reg_set)
2551                                                ) == 0) {
2552                /* Hardware may not set outbound_intr_status in MSI-X mode */
2553                if (!instance->msix_vectors)
2554                        return IRQ_NONE;
2555        }
2556
2557        instance->mfiStatus = mfiStatus;
2558
2559        if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2560                fw_state = instance->instancet->read_fw_status_reg(
2561                                instance->reg_set) & MFI_STATE_MASK;
2562
2563                if (fw_state != MFI_STATE_FAULT) {
2564                        printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2565                                                fw_state);
2566                }
2567
2568                if ((fw_state == MFI_STATE_FAULT) &&
2569                                (instance->disableOnlineCtrlReset == 0)) {
2570                        printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2571
2572                        if ((instance->pdev->device ==
2573                                        PCI_DEVICE_ID_LSI_SAS1064R) ||
2574                                (instance->pdev->device ==
2575                                        PCI_DEVICE_ID_DELL_PERC5) ||
2576                                (instance->pdev->device ==
2577                                        PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2578
2579                                *instance->consumer =
2580                                        MEGASAS_ADPRESET_INPROG_SIGN;
2581                        }
2582
2583
2584                        instance->instancet->disable_intr(instance->reg_set);
2585                        instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
2586                        instance->issuepend_done = 0;
2587
2588                        atomic_set(&instance->fw_outstanding, 0);
2589                        megasas_internal_reset_defer_cmds(instance);
2590
2591                        printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2592                                        fw_state, instance->adprecovery);
2593
2594                        schedule_work(&instance->work_init);
2595                        return IRQ_HANDLED;
2596
2597                } else {
2598                        printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2599                                fw_state, instance->disableOnlineCtrlReset);
2600                }
2601        }
2602
2603        tasklet_schedule(&instance->isr_tasklet);
2604        return IRQ_HANDLED;
2605}
2606/**
2607 * megasas_isr - isr entry point
2608 */
2609static irqreturn_t megasas_isr(int irq, void *devp)
2610{
2611        struct megasas_irq_context *irq_context = devp;
2612        struct megasas_instance *instance = irq_context->instance;
2613        unsigned long flags;
2614        irqreturn_t     rc;
2615
2616        if (atomic_read(&instance->fw_reset_no_pci_access))
2617                return IRQ_HANDLED;
2618
2619        spin_lock_irqsave(&instance->hba_lock, flags);
2620        rc =  megasas_deplete_reply_queue(instance, DID_OK);
2621        spin_unlock_irqrestore(&instance->hba_lock, flags);
2622
2623        return rc;
2624}
2625
2626/**
2627 * megasas_transition_to_ready -        Move the FW to READY state
2628 * @instance:                           Adapter soft state
2629 *
2630 * During the initialization, FW passes can potentially be in any one of
2631 * several possible states. If the FW in operational, waiting-for-handshake
2632 * states, driver must take steps to bring it to ready state. Otherwise, it
2633 * has to wait for the ready state.
2634 */
2635int
2636megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
2637{
2638        int i;
2639        u8 max_wait;
2640        u32 fw_state;
2641        u32 cur_state;
2642        u32 abs_state, curr_abs_state;
2643
2644        fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2645
2646        if (fw_state != MFI_STATE_READY)
2647                printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2648                       " state\n");
2649
2650        while (fw_state != MFI_STATE_READY) {
2651
2652                abs_state =
2653                instance->instancet->read_fw_status_reg(instance->reg_set);
2654
2655                switch (fw_state) {
2656
2657                case MFI_STATE_FAULT:
2658                        printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2659                        if (ocr) {
2660                                max_wait = MEGASAS_RESET_WAIT_TIME;
2661                                cur_state = MFI_STATE_FAULT;
2662                                break;
2663                        } else
2664                                return -ENODEV;
2665
2666                case MFI_STATE_WAIT_HANDSHAKE:
2667                        /*
2668                         * Set the CLR bit in inbound doorbell
2669                         */
2670                        if ((instance->pdev->device ==
2671                                PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2672                                (instance->pdev->device ==
2673                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2674                                (instance->pdev->device ==
2675                                 PCI_DEVICE_ID_LSI_FUSION) ||
2676                                (instance->pdev->device ==
2677                                PCI_DEVICE_ID_LSI_INVADER)) {
2678                                writel(
2679                                  MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2680                                  &instance->reg_set->doorbell);
2681                        } else {
2682                                writel(
2683                                    MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2684                                        &instance->reg_set->inbound_doorbell);
2685                        }
2686
2687                        max_wait = MEGASAS_RESET_WAIT_TIME;
2688                        cur_state = MFI_STATE_WAIT_HANDSHAKE;
2689                        break;
2690
2691                case MFI_STATE_BOOT_MESSAGE_PENDING:
2692                        if ((instance->pdev->device ==
2693                             PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2694                                (instance->pdev->device ==
2695                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2696                            (instance->pdev->device ==
2697                             PCI_DEVICE_ID_LSI_FUSION) ||
2698                            (instance->pdev->device ==
2699                             PCI_DEVICE_ID_LSI_INVADER)) {
2700                                writel(MFI_INIT_HOTPLUG,
2701                                       &instance->reg_set->doorbell);
2702                        } else
2703                                writel(MFI_INIT_HOTPLUG,
2704                                        &instance->reg_set->inbound_doorbell);
2705
2706                        max_wait = MEGASAS_RESET_WAIT_TIME;
2707                        cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2708                        break;
2709
2710                case MFI_STATE_OPERATIONAL:
2711                        /*
2712                         * Bring it to READY state; assuming max wait 10 secs
2713                         */
2714                        instance->instancet->disable_intr(instance->reg_set);
2715                        if ((instance->pdev->device ==
2716                                PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2717                                (instance->pdev->device ==
2718                                PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
2719                                (instance->pdev->device
2720                                        == PCI_DEVICE_ID_LSI_FUSION) ||
2721                                (instance->pdev->device
2722                                        == PCI_DEVICE_ID_LSI_INVADER)) {
2723                                writel(MFI_RESET_FLAGS,
2724                                        &instance->reg_set->doorbell);
2725                                if ((instance->pdev->device ==
2726                                    PCI_DEVICE_ID_LSI_FUSION) ||
2727                                    (instance->pdev->device ==
2728                                     PCI_DEVICE_ID_LSI_INVADER)) {
2729                                        for (i = 0; i < (10 * 1000); i += 20) {
2730                                                if (readl(
2731                                                            &instance->
2732                                                            reg_set->
2733                                                            doorbell) & 1)
2734                                                        msleep(20);
2735                                                else
2736                                                        break;
2737                                        }
2738                                }
2739                        } else
2740                                writel(MFI_RESET_FLAGS,
2741                                        &instance->reg_set->inbound_doorbell);
2742
2743                        max_wait = MEGASAS_RESET_WAIT_TIME;
2744                        cur_state = MFI_STATE_OPERATIONAL;
2745                        break;
2746
2747                case MFI_STATE_UNDEFINED:
2748                        /*
2749                         * This state should not last for more than 2 seconds
2750                         */
2751                        max_wait = MEGASAS_RESET_WAIT_TIME;
2752                        cur_state = MFI_STATE_UNDEFINED;
2753                        break;
2754
2755                case MFI_STATE_BB_INIT:
2756                        max_wait = MEGASAS_RESET_WAIT_TIME;
2757                        cur_state = MFI_STATE_BB_INIT;
2758                        break;
2759
2760                case MFI_STATE_FW_INIT:
2761                        max_wait = MEGASAS_RESET_WAIT_TIME;
2762                        cur_state = MFI_STATE_FW_INIT;
2763                        break;
2764
2765                case MFI_STATE_FW_INIT_2:
2766                        max_wait = MEGASAS_RESET_WAIT_TIME;
2767                        cur_state = MFI_STATE_FW_INIT_2;
2768                        break;
2769
2770                case MFI_STATE_DEVICE_SCAN:
2771                        max_wait = MEGASAS_RESET_WAIT_TIME;
2772                        cur_state = MFI_STATE_DEVICE_SCAN;
2773                        break;
2774
2775                case MFI_STATE_FLUSH_CACHE:
2776                        max_wait = MEGASAS_RESET_WAIT_TIME;
2777                        cur_state = MFI_STATE_FLUSH_CACHE;
2778                        break;
2779
2780                default:
2781                        printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2782                               fw_state);
2783                        return -ENODEV;
2784                }
2785
2786                /*
2787                 * The cur_state should not last for more than max_wait secs
2788                 */
2789                for (i = 0; i < (max_wait * 1000); i++) {
2790                        fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2791                                        MFI_STATE_MASK ;
2792                curr_abs_state =
2793                instance->instancet->read_fw_status_reg(instance->reg_set);
2794
2795                        if (abs_state == curr_abs_state) {
2796                                msleep(1);
2797                        } else
2798                                break;
2799                }
2800
2801                /*
2802                 * Return error if fw_state hasn't changed after max_wait
2803                 */
2804                if (curr_abs_state == abs_state) {
2805                        printk(KERN_DEBUG "FW state [%d] hasn't changed "
2806                               "in %d secs\n", fw_state, max_wait);
2807                        return -ENODEV;
2808                }
2809        }
2810        printk(KERN_INFO "megasas: FW now in Ready state\n");
2811
2812        return 0;
2813}
2814
2815/**
2816 * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
2817 * @instance:                           Adapter soft state
2818 */
2819static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2820{
2821        int i;
2822        u32 max_cmd = instance->max_mfi_cmds;
2823        struct megasas_cmd *cmd;
2824
2825        if (!instance->frame_dma_pool)
2826                return;
2827
2828        /*
2829         * Return all frames to pool
2830         */
2831        for (i = 0; i < max_cmd; i++) {
2832
2833                cmd = instance->cmd_list[i];
2834
2835                if (cmd->frame)
2836                        pci_pool_free(instance->frame_dma_pool, cmd->frame,
2837                                      cmd->frame_phys_addr);
2838
2839                if (cmd->sense)
2840                        pci_pool_free(instance->sense_dma_pool, cmd->sense,
2841                                      cmd->sense_phys_addr);
2842        }
2843
2844        /*
2845         * Now destroy the pool itself
2846         */
2847        pci_pool_destroy(instance->frame_dma_pool);
2848        pci_pool_destroy(instance->sense_dma_pool);
2849
2850        instance->frame_dma_pool = NULL;
2851        instance->sense_dma_pool = NULL;
2852}
2853
2854/**
2855 * megasas_create_frame_pool -  Creates DMA pool for cmd frames
2856 * @instance:                   Adapter soft state
2857 *
2858 * Each command packet has an embedded DMA memory buffer that is used for
2859 * filling MFI frame and the SG list that immediately follows the frame. This
2860 * function creates those DMA memory buffers for each command packet by using
2861 * PCI pool facility.
2862 */
2863static int megasas_create_frame_pool(struct megasas_instance *instance)
2864{
2865        int i;
2866        u32 max_cmd;
2867        u32 sge_sz;
2868        u32 sgl_sz;
2869        u32 total_sz;
2870        u32 frame_count;
2871        struct megasas_cmd *cmd;
2872
2873        max_cmd = instance->max_mfi_cmds;
2874
2875        /*
2876         * Size of our frame is 64 bytes for MFI frame, followed by max SG
2877         * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2878         */
2879        sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2880            sizeof(struct megasas_sge32);
2881
2882        if (instance->flag_ieee) {
2883                sge_sz = sizeof(struct megasas_sge_skinny);
2884        }
2885
2886        /*
2887         * Calculated the number of 64byte frames required for SGL
2888         */
2889        sgl_sz = sge_sz * instance->max_num_sge;
2890        frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2891        frame_count = 15;
2892
2893        /*
2894         * We need one extra frame for the MFI command
2895         */
2896        frame_count++;
2897
2898        total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2899        /*
2900         * Use DMA pool facility provided by PCI layer
2901         */
2902        instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2903                                                   instance->pdev, total_sz, 64,
2904                                                   0);
2905
2906        if (!instance->frame_dma_pool) {
2907                printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2908                return -ENOMEM;
2909        }
2910
2911        instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2912                                                   instance->pdev, 128, 4, 0);
2913
2914        if (!instance->sense_dma_pool) {
2915                printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2916
2917                pci_pool_destroy(instance->frame_dma_pool);
2918                instance->frame_dma_pool = NULL;
2919
2920                return -ENOMEM;
2921        }
2922
2923        /*
2924         * Allocate and attach a frame to each of the commands in cmd_list.
2925         * By making cmd->index as the context instead of the &cmd, we can
2926         * always use 32bit context regardless of the architecture
2927         */
2928        for (i = 0; i < max_cmd; i++) {
2929
2930                cmd = instance->cmd_list[i];
2931
2932                cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2933                                            GFP_KERNEL, &cmd->frame_phys_addr);
2934
2935                cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2936                                            GFP_KERNEL, &cmd->sense_phys_addr);
2937
2938                /*
2939                 * megasas_teardown_frame_pool() takes care of freeing
2940                 * whatever has been allocated
2941                 */
2942                if (!cmd->frame || !cmd->sense) {
2943                        printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2944                        megasas_teardown_frame_pool(instance);
2945                        return -ENOMEM;
2946                }
2947
2948                memset(cmd->frame, 0, total_sz);
2949                cmd->frame->io.context = cmd->index;
2950                cmd->frame->io.pad_0 = 0;
2951                if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2952                    (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2953                    (reset_devices))
2954                        cmd->frame->hdr.cmd = MFI_CMD_INVALID;
2955        }
2956
2957        return 0;
2958}
2959
2960/**
2961 * megasas_free_cmds -  Free all the cmds in the free cmd pool
2962 * @instance:           Adapter soft state
2963 */
2964void megasas_free_cmds(struct megasas_instance *instance)
2965{
2966        int i;
2967        /* First free the MFI frame pool */
2968        megasas_teardown_frame_pool(instance);
2969
2970        /* Free all the commands in the cmd_list */
2971        for (i = 0; i < instance->max_mfi_cmds; i++)
2972
2973                kfree(instance->cmd_list[i]);
2974
2975        /* Free the cmd_list buffer itself */
2976        kfree(instance->cmd_list);
2977        instance->cmd_list = NULL;
2978
2979        INIT_LIST_HEAD(&instance->cmd_pool);
2980}
2981
2982/**
2983 * megasas_alloc_cmds - Allocates the command packets
2984 * @instance:           Adapter soft state
2985 *
2986 * Each command that is issued to the FW, whether IO commands from the OS or
2987 * internal commands like IOCTLs, are wrapped in local data structure called
2988 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2989 * the FW.
2990 *
2991 * Each frame has a 32-bit field called context (tag). This context is used
2992 * to get back the megasas_cmd from the frame when a frame gets completed in
2993 * the ISR. Typically the address of the megasas_cmd itself would be used as
2994 * the context. But we wanted to keep the differences between 32 and 64 bit
2995 * systems to the mininum. We always use 32 bit integers for the context. In
2996 * this driver, the 32 bit values are the indices into an array cmd_list.
2997 * This array is used only to look up the megasas_cmd given the context. The
2998 * free commands themselves are maintained in a linked list called cmd_pool.
2999 */
3000int megasas_alloc_cmds(struct megasas_instance *instance)
3001{
3002        int i;
3003        int j;
3004        u32 max_cmd;
3005        struct megasas_cmd *cmd;
3006
3007        max_cmd = instance->max_mfi_cmds;
3008
3009        /*
3010         * instance->cmd_list is an array of struct megasas_cmd pointers.
3011         * Allocate the dynamic array first and then allocate individual
3012         * commands.
3013         */
3014        instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3015
3016        if (!instance->cmd_list) {
3017                printk(KERN_DEBUG "megasas: out of memory\n");
3018                return -ENOMEM;
3019        }
3020
3021        memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3022
3023        for (i = 0; i < max_cmd; i++) {
3024                instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3025                                                GFP_KERNEL);
3026
3027                if (!instance->cmd_list[i]) {
3028
3029                        for (j = 0; j < i; j++)
3030                                kfree(instance->cmd_list[j]);
3031
3032                        kfree(instance->cmd_list);
3033                        instance->cmd_list = NULL;
3034
3035                        return -ENOMEM;
3036                }
3037        }
3038
3039        /*
3040         * Add all the commands to command pool (instance->cmd_pool)
3041         */
3042        for (i = 0; i < max_cmd; i++) {
3043                cmd = instance->cmd_list[i];
3044                memset(cmd, 0, sizeof(struct megasas_cmd));
3045                cmd->index = i;
3046                cmd->scmd = NULL;
3047                cmd->instance = instance;
3048
3049                list_add_tail(&cmd->list, &instance->cmd_pool);
3050        }
3051
3052        /*
3053         * Create a frame pool and assign one frame to each cmd
3054         */
3055        if (megasas_create_frame_pool(instance)) {
3056                printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3057                megasas_free_cmds(instance);
3058        }
3059
3060        return 0;
3061}
3062
3063/*
3064 * megasas_get_pd_list_info -   Returns FW's pd_list structure
3065 * @instance:                           Adapter soft state
3066 * @pd_list:                            pd_list structure
3067 *
3068 * Issues an internal command (DCMD) to get the FW's controller PD
3069 * list structure.  This information is mainly used to find out SYSTEM
3070 * supported by the FW.
3071 */
3072static int
3073megasas_get_pd_list(struct megasas_instance *instance)
3074{
3075        int ret = 0, pd_index = 0;
3076        struct megasas_cmd *cmd;
3077        struct megasas_dcmd_frame *dcmd;
3078        struct MR_PD_LIST *ci;
3079        struct MR_PD_ADDRESS *pd_addr;
3080        dma_addr_t ci_h = 0;
3081
3082        cmd = megasas_get_cmd(instance);
3083
3084        if (!cmd) {
3085                printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3086                return -ENOMEM;
3087        }
3088
3089        dcmd = &cmd->frame->dcmd;
3090
3091        ci = pci_alloc_consistent(instance->pdev,
3092                  MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3093
3094        if (!ci) {
3095                printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3096                megasas_return_cmd(instance, cmd);
3097                return -ENOMEM;
3098        }
3099
3100        memset(ci, 0, sizeof(*ci));
3101        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3102
3103        dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3104        dcmd->mbox.b[1] = 0;
3105        dcmd->cmd = MFI_CMD_DCMD;
3106        dcmd->cmd_status = 0xFF;
3107        dcmd->sge_count = 1;
3108        dcmd->flags = MFI_FRAME_DIR_READ;
3109        dcmd->timeout = 0;
3110        dcmd->pad_0 = 0;
3111        dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3112        dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3113        dcmd->sgl.sge32[0].phys_addr = ci_h;
3114        dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3115
3116        if (!megasas_issue_polled(instance, cmd)) {
3117                ret = 0;
3118        } else {
3119                ret = -1;
3120        }
3121
3122        /*
3123        * the following function will get the instance PD LIST.
3124        */
3125
3126        pd_addr = ci->addr;
3127
3128        if ( ret == 0 &&
3129                (ci->count <
3130                  (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3131
3132                memset(instance->pd_list, 0,
3133                        MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3134
3135                for (pd_index = 0; pd_index < ci->count; pd_index++) {
3136
3137                        instance->pd_list[pd_addr->deviceId].tid        =
3138                                                        pd_addr->deviceId;
3139                        instance->pd_list[pd_addr->deviceId].driveType  =
3140                                                        pd_addr->scsiDevType;
3141                        instance->pd_list[pd_addr->deviceId].driveState =
3142                                                        MR_PD_STATE_SYSTEM;
3143                        pd_addr++;
3144                }
3145        }
3146
3147        pci_free_consistent(instance->pdev,
3148                                MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3149                                ci, ci_h);
3150        megasas_return_cmd(instance, cmd);
3151
3152        return ret;
3153}
3154
3155/*
3156 * megasas_get_ld_list_info -   Returns FW's ld_list structure
3157 * @instance:                           Adapter soft state
3158 * @ld_list:                            ld_list structure
3159 *
3160 * Issues an internal command (DCMD) to get the FW's controller PD
3161 * list structure.  This information is mainly used to find out SYSTEM
3162 * supported by the FW.
3163 */
3164static int
3165megasas_get_ld_list(struct megasas_instance *instance)
3166{
3167        int ret = 0, ld_index = 0, ids = 0;
3168        struct megasas_cmd *cmd;
3169        struct megasas_dcmd_frame *dcmd;
3170        struct MR_LD_LIST *ci;
3171        dma_addr_t ci_h = 0;
3172
3173        cmd = megasas_get_cmd(instance);
3174
3175        if (!cmd) {
3176                printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3177                return -ENOMEM;
3178        }
3179
3180        dcmd = &cmd->frame->dcmd;
3181
3182        ci = pci_alloc_consistent(instance->pdev,
3183                                sizeof(struct MR_LD_LIST),
3184                                &ci_h);
3185
3186        if (!ci) {
3187                printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3188                megasas_return_cmd(instance, cmd);
3189                return -ENOMEM;
3190        }
3191
3192        memset(ci, 0, sizeof(*ci));
3193        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3194
3195        dcmd->cmd = MFI_CMD_DCMD;
3196        dcmd->cmd_status = 0xFF;
3197        dcmd->sge_count = 1;
3198        dcmd->flags = MFI_FRAME_DIR_READ;
3199        dcmd->timeout = 0;
3200        dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3201        dcmd->opcode = MR_DCMD_LD_GET_LIST;
3202        dcmd->sgl.sge32[0].phys_addr = ci_h;
3203        dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3204        dcmd->pad_0  = 0;
3205
3206        if (!megasas_issue_polled(instance, cmd)) {
3207                ret = 0;
3208        } else {
3209                ret = -1;
3210        }
3211
3212        /* the following function will get the instance PD LIST */
3213
3214        if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3215                memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3216
3217                for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3218                        if (ci->ldList[ld_index].state != 0) {
3219                                ids = ci->ldList[ld_index].ref.targetId;
3220                                instance->ld_ids[ids] =
3221                                        ci->ldList[ld_index].ref.targetId;
3222                        }
3223                }
3224        }
3225
3226        pci_free_consistent(instance->pdev,
3227                                sizeof(struct MR_LD_LIST),
3228                                ci,
3229                                ci_h);
3230
3231        megasas_return_cmd(instance, cmd);
3232        return ret;
3233}
3234
3235/**
3236 * megasas_get_controller_info -        Returns FW's controller structure
3237 * @instance:                           Adapter soft state
3238 * @ctrl_info:                          Controller information structure
3239 *
3240 * Issues an internal command (DCMD) to get the FW's controller structure.
3241 * This information is mainly used to find out the maximum IO transfer per
3242 * command supported by the FW.
3243 */
3244static int
3245megasas_get_ctrl_info(struct megasas_instance *instance,
3246                      struct megasas_ctrl_info *ctrl_info)
3247{
3248        int ret = 0;
3249        struct megasas_cmd *cmd;
3250        struct megasas_dcmd_frame *dcmd;
3251        struct megasas_ctrl_info *ci;
3252        dma_addr_t ci_h = 0;
3253
3254        cmd = megasas_get_cmd(instance);
3255
3256        if (!cmd) {
3257                printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3258                return -ENOMEM;
3259        }
3260
3261        dcmd = &cmd->frame->dcmd;
3262
3263        ci = pci_alloc_consistent(instance->pdev,
3264                                  sizeof(struct megasas_ctrl_info), &ci_h);
3265
3266        if (!ci) {
3267                printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3268                megasas_return_cmd(instance, cmd);
3269                return -ENOMEM;
3270        }
3271
3272        memset(ci, 0, sizeof(*ci));
3273        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3274
3275        dcmd->cmd = MFI_CMD_DCMD;
3276        dcmd->cmd_status = 0xFF;
3277        dcmd->sge_count = 1;
3278        dcmd->flags = MFI_FRAME_DIR_READ;
3279        dcmd->timeout = 0;
3280        dcmd->pad_0 = 0;
3281        dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3282        dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3283        dcmd->sgl.sge32[0].phys_addr = ci_h;
3284        dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3285
3286        if (!megasas_issue_polled(instance, cmd)) {
3287                ret = 0;
3288                memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3289        } else {
3290                ret = -1;
3291        }
3292
3293        pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3294                            ci, ci_h);
3295
3296        megasas_return_cmd(instance, cmd);
3297        return ret;
3298}
3299
3300/**
3301 * megasas_issue_init_mfi -     Initializes the FW
3302 * @instance:           Adapter soft state
3303 *
3304 * Issues the INIT MFI cmd
3305 */
3306static int
3307megasas_issue_init_mfi(struct megasas_instance *instance)
3308{
3309        u32 context;
3310
3311        struct megasas_cmd *cmd;
3312
3313        struct megasas_init_frame *init_frame;
3314        struct megasas_init_queue_info *initq_info;
3315        dma_addr_t init_frame_h;
3316        dma_addr_t initq_info_h;
3317
3318        /*
3319         * Prepare a init frame. Note the init frame points to queue info
3320         * structure. Each frame has SGL allocated after first 64 bytes. For
3321         * this frame - since we don't need any SGL - we use SGL's space as
3322         * queue info structure
3323         *
3324         * We will not get a NULL command below. We just created the pool.
3325         */
3326        cmd = megasas_get_cmd(instance);
3327
3328        init_frame = (struct megasas_init_frame *)cmd->frame;
3329        initq_info = (struct megasas_init_queue_info *)
3330                ((unsigned long)init_frame + 64);
3331
3332        init_frame_h = cmd->frame_phys_addr;
3333        initq_info_h = init_frame_h + 64;
3334
3335        context = init_frame->context;
3336        memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3337        memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3338        init_frame->context = context;
3339
3340        initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3341        initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3342
3343        initq_info->producer_index_phys_addr_lo = instance->producer_h;
3344        initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3345
3346        init_frame->cmd = MFI_CMD_INIT;
3347        init_frame->cmd_status = 0xFF;
3348        init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3349
3350        init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3351
3352        /*
3353         * disable the intr before firing the init frame to FW
3354         */
3355        instance->instancet->disable_intr(instance->reg_set);
3356
3357        /*
3358         * Issue the init frame in polled mode
3359         */
3360
3361        if (megasas_issue_polled(instance, cmd)) {
3362                printk(KERN_ERR "megasas: Failed to init firmware\n");
3363                megasas_return_cmd(instance, cmd);
3364                goto fail_fw_init;
3365        }
3366
3367        megasas_return_cmd(instance, cmd);
3368
3369        return 0;
3370
3371fail_fw_init:
3372        return -EINVAL;
3373}
3374
3375static u32
3376megasas_init_adapter_mfi(struct megasas_instance *instance)
3377{
3378        struct megasas_register_set __iomem *reg_set;
3379        u32 context_sz;
3380        u32 reply_q_sz;
3381
3382        reg_set = instance->reg_set;
3383
3384        /*
3385         * Get various operational parameters from status register
3386         */
3387        instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3388        /*
3389         * Reduce the max supported cmds by 1. This is to ensure that the
3390         * reply_q_sz (1 more than the max cmd that driver may send)
3391         * does not exceed max cmds that the FW can support
3392         */
3393        instance->max_fw_cmds = instance->max_fw_cmds-1;
3394        instance->max_mfi_cmds = instance->max_fw_cmds;
3395        instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3396                                        0x10;
3397        /*
3398         * Create a pool of commands
3399         */
3400        if (megasas_alloc_cmds(instance))
3401                goto fail_alloc_cmds;
3402
3403        /*
3404         * Allocate memory for reply queue. Length of reply queue should
3405         * be _one_ more than the maximum commands handled by the firmware.
3406         *
3407         * Note: When FW completes commands, it places corresponding contex
3408         * values in this circular reply queue. This circular queue is a fairly
3409         * typical producer-consumer queue. FW is the producer (of completed
3410         * commands) and the driver is the consumer.
3411         */
3412        context_sz = sizeof(u32);
3413        reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3414
3415        instance->reply_queue = pci_alloc_consistent(instance->pdev,
3416                                                     reply_q_sz,
3417                                                     &instance->reply_queue_h);
3418
3419        if (!instance->reply_queue) {
3420                printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3421                goto fail_reply_queue;
3422        }
3423
3424        if (megasas_issue_init_mfi(instance))
3425                goto fail_fw_init;
3426
3427        instance->fw_support_ieee = 0;
3428        instance->fw_support_ieee =
3429                (instance->instancet->read_fw_status_reg(reg_set) &
3430                0x04000000);
3431
3432        printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3433                        instance->fw_support_ieee);
3434
3435        if (instance->fw_support_ieee)
3436                instance->flag_ieee = 1;
3437
3438        return 0;
3439
3440fail_fw_init:
3441
3442        pci_free_consistent(instance->pdev, reply_q_sz,
3443                            instance->reply_queue, instance->reply_queue_h);
3444fail_reply_queue:
3445        megasas_free_cmds(instance);
3446
3447fail_alloc_cmds:
3448        return 1;
3449}
3450
3451/**
3452 * megasas_init_fw -    Initializes the FW
3453 * @instance:           Adapter soft state
3454 *
3455 * This is the main function for initializing firmware
3456 */
3457
3458static int megasas_init_fw(struct megasas_instance *instance)
3459{
3460        u32 max_sectors_1;
3461        u32 max_sectors_2;
3462        u32 tmp_sectors, msix_enable;
3463        struct megasas_register_set __iomem *reg_set;
3464        struct megasas_ctrl_info *ctrl_info;
3465        unsigned long bar_list;
3466        int i;
3467
3468        /* Find first memory bar */
3469        bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3470        instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3471        instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3472        if (pci_request_selected_regions(instance->pdev, instance->bar,
3473                                         "megasas: LSI")) {
3474                printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3475                return -EBUSY;
3476        }
3477
3478        instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3479
3480        if (!instance->reg_set) {
3481                printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3482                goto fail_ioremap;
3483        }
3484
3485        reg_set = instance->reg_set;
3486
3487        switch (instance->pdev->device) {
3488        case PCI_DEVICE_ID_LSI_FUSION:
3489        case PCI_DEVICE_ID_LSI_INVADER:
3490                instance->instancet = &megasas_instance_template_fusion;
3491                break;
3492        case PCI_DEVICE_ID_LSI_SAS1078R:
3493        case PCI_DEVICE_ID_LSI_SAS1078DE:
3494                instance->instancet = &megasas_instance_template_ppc;
3495                break;
3496        case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3497        case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3498                instance->instancet = &megasas_instance_template_gen2;
3499                break;
3500        case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3501        case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3502                instance->instancet = &megasas_instance_template_skinny;
3503                break;
3504        case PCI_DEVICE_ID_LSI_SAS1064R:
3505        case PCI_DEVICE_ID_DELL_PERC5:
3506        default:
3507                instance->instancet = &megasas_instance_template_xscale;
3508                break;
3509        }
3510
3511        /*
3512         * We expect the FW state to be READY
3513         */
3514        if (megasas_transition_to_ready(instance, 0))
3515                goto fail_ready_state;
3516
3517        /* Check if MSI-X is supported while in ready state */
3518        msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3519                       0x4000000) >> 0x1a;
3520        if (msix_enable && !msix_disable) {
3521                /* Check max MSI-X vectors */
3522                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3523                    (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3524                        instance->msix_vectors = (readl(&instance->reg_set->
3525                                                        outbound_scratch_pad_2
3526                                                          ) & 0x1F) + 1;
3527                        if (msix_vectors)
3528                                instance->msix_vectors =
3529                                        min(msix_vectors,
3530                                            instance->msix_vectors);
3531                } else
3532                        instance->msix_vectors = 1;
3533                /* Don't bother allocating more MSI-X vectors than cpus */
3534                instance->msix_vectors = min(instance->msix_vectors,
3535                                             (unsigned int)num_online_cpus());
3536                for (i = 0; i < instance->msix_vectors; i++)
3537                        instance->msixentry[i].entry = i;
3538                i = pci_enable_msix(instance->pdev, instance->msixentry,
3539                                    instance->msix_vectors);
3540                if (i >= 0) {
3541                        if (i) {
3542                                if (!pci_enable_msix(instance->pdev,
3543                                                     instance->msixentry, i))
3544                                        instance->msix_vectors = i;
3545                                else
3546                                        instance->msix_vectors = 0;
3547                        }
3548                } else
3549                        instance->msix_vectors = 0;
3550        }
3551
3552        /* Get operational params, sge flags, send init cmd to controller */
3553        if (instance->instancet->init_adapter(instance))
3554                goto fail_init_adapter;
3555
3556        printk(KERN_ERR "megasas: INIT adapter done\n");
3557
3558        /** for passthrough
3559        * the following function will get the PD LIST.
3560        */
3561
3562        memset(instance->pd_list, 0 ,
3563                (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3564        megasas_get_pd_list(instance);
3565
3566        memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3567        megasas_get_ld_list(instance);
3568
3569        ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3570
3571        /*
3572         * Compute the max allowed sectors per IO: The controller info has two
3573         * limits on max sectors. Driver should use the minimum of these two.
3574         *
3575         * 1 << stripe_sz_ops.min = max sectors per strip
3576         *
3577         * Note that older firmwares ( < FW ver 30) didn't report information
3578         * to calculate max_sectors_1. So the number ended up as zero always.
3579         */
3580        tmp_sectors = 0;
3581        if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3582
3583                max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3584                    ctrl_info->max_strips_per_io;
3585                max_sectors_2 = ctrl_info->max_request_size;
3586
3587                tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3588                instance->disableOnlineCtrlReset =
3589                ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3590        }
3591
3592        instance->max_sectors_per_req = instance->max_num_sge *
3593                                                PAGE_SIZE / 512;
3594        if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3595                instance->max_sectors_per_req = tmp_sectors;
3596
3597        kfree(ctrl_info);
3598
3599        /* Check for valid throttlequeuedepth module parameter */
3600        if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3601            instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3602                if (throttlequeuedepth > (instance->max_fw_cmds -
3603                                          MEGASAS_SKINNY_INT_CMDS))
3604                        instance->throttlequeuedepth =
3605                                MEGASAS_THROTTLE_QUEUE_DEPTH;
3606                else
3607                        instance->throttlequeuedepth = throttlequeuedepth;
3608        } else {
3609                if (throttlequeuedepth > (instance->max_fw_cmds -
3610                                          MEGASAS_INT_CMDS))
3611                        instance->throttlequeuedepth =
3612                                MEGASAS_THROTTLE_QUEUE_DEPTH;
3613                else
3614                        instance->throttlequeuedepth = throttlequeuedepth;
3615        }
3616
3617        /*
3618        * Setup tasklet for cmd completion
3619        */
3620
3621        tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3622                (unsigned long)instance);
3623
3624        return 0;
3625
3626fail_init_adapter:
3627fail_ready_state:
3628        iounmap(instance->reg_set);
3629
3630      fail_ioremap:
3631        pci_release_selected_regions(instance->pdev, instance->bar);
3632
3633        return -EINVAL;
3634}
3635
3636/**
3637 * megasas_release_mfi -        Reverses the FW initialization
3638 * @intance:                    Adapter soft state
3639 */
3640static void megasas_release_mfi(struct megasas_instance *instance)
3641{
3642        u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3643
3644        if (instance->reply_queue)
3645                pci_free_consistent(instance->pdev, reply_q_sz,
3646                            instance->reply_queue, instance->reply_queue_h);
3647
3648        megasas_free_cmds(instance);
3649
3650        iounmap(instance->reg_set);
3651
3652        pci_release_selected_regions(instance->pdev, instance->bar);
3653}
3654
3655/**
3656 * megasas_get_seq_num -        Gets latest event sequence numbers
3657 * @instance:                   Adapter soft state
3658 * @eli:                        FW event log sequence numbers information
3659 *
3660 * FW maintains a log of all events in a non-volatile area. Upper layers would
3661 * usually find out the latest sequence number of the events, the seq number at
3662 * the boot etc. They would "read" all the events below the latest seq number
3663 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3664 * number), they would subsribe to AEN (asynchronous event notification) and
3665 * wait for the events to happen.
3666 */
3667static int
3668megasas_get_seq_num(struct megasas_instance *instance,
3669                    struct megasas_evt_log_info *eli)
3670{
3671        struct megasas_cmd *cmd;
3672        struct megasas_dcmd_frame *dcmd;
3673        struct megasas_evt_log_info *el_info;
3674        dma_addr_t el_info_h = 0;
3675
3676        cmd = megasas_get_cmd(instance);
3677
3678        if (!cmd) {
3679                return -ENOMEM;
3680        }
3681
3682        dcmd = &cmd->frame->dcmd;
3683        el_info = pci_alloc_consistent(instance->pdev,
3684                                       sizeof(struct megasas_evt_log_info),
3685                                       &el_info_h);
3686
3687        if (!el_info) {
3688                megasas_return_cmd(instance, cmd);
3689                return -ENOMEM;
3690        }
3691
3692        memset(el_info, 0, sizeof(*el_info));
3693        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3694
3695        dcmd->cmd = MFI_CMD_DCMD;
3696        dcmd->cmd_status = 0x0;
3697        dcmd->sge_count = 1;
3698        dcmd->flags = MFI_FRAME_DIR_READ;
3699        dcmd->timeout = 0;
3700        dcmd->pad_0 = 0;
3701        dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3702        dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3703        dcmd->sgl.sge32[0].phys_addr = el_info_h;
3704        dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3705
3706        megasas_issue_blocked_cmd(instance, cmd);
3707
3708        /*
3709         * Copy the data back into callers buffer
3710         */
3711        memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3712
3713        pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3714                            el_info, el_info_h);
3715
3716        megasas_return_cmd(instance, cmd);
3717
3718        return 0;
3719}
3720
3721/**
3722 * megasas_register_aen -       Registers for asynchronous event notification
3723 * @instance:                   Adapter soft state
3724 * @seq_num:                    The starting sequence number
3725 * @class_locale:               Class of the event
3726 *
3727 * This function subscribes for AEN for events beyond the @seq_num. It requests
3728 * to be notified if and only if the event is of type @class_locale
3729 */
3730static int
3731megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3732                     u32 class_locale_word)
3733{
3734        int ret_val;
3735        struct megasas_cmd *cmd;
3736        struct megasas_dcmd_frame *dcmd;
3737        union megasas_evt_class_locale curr_aen;
3738        union megasas_evt_class_locale prev_aen;
3739
3740        /*
3741         * If there an AEN pending already (aen_cmd), check if the
3742         * class_locale of that pending AEN is inclusive of the new
3743         * AEN request we currently have. If it is, then we don't have
3744         * to do anything. In other words, whichever events the current
3745         * AEN request is subscribing to, have already been subscribed
3746         * to.
3747         *
3748         * If the old_cmd is _not_ inclusive, then we have to abort
3749         * that command, form a class_locale that is superset of both
3750         * old and current and re-issue to the FW
3751         */
3752
3753        curr_aen.word = class_locale_word;
3754
3755        if (instance->aen_cmd) {
3756
3757                prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3758
3759                /*
3760                 * A class whose enum value is smaller is inclusive of all
3761                 * higher values. If a PROGRESS (= -1) was previously
3762                 * registered, then a new registration requests for higher
3763                 * classes need not be sent to FW. They are automatically
3764                 * included.
3765                 *
3766                 * Locale numbers don't have such hierarchy. They are bitmap
3767                 * values
3768                 */
3769                if ((prev_aen.members.class <= curr_aen.members.class) &&
3770                    !((prev_aen.members.locale & curr_aen.members.locale) ^
3771                      curr_aen.members.locale)) {
3772                        /*
3773                         * Previously issued event registration includes
3774                         * current request. Nothing to do.
3775                         */
3776                        return 0;
3777                } else {
3778                        curr_aen.members.locale |= prev_aen.members.locale;
3779
3780                        if (prev_aen.members.class < curr_aen.members.class)
3781                                curr_aen.members.class = prev_aen.members.class;
3782
3783                        instance->aen_cmd->abort_aen = 1;
3784                        ret_val = megasas_issue_blocked_abort_cmd(instance,
3785                                                                  instance->
3786                                                                  aen_cmd);
3787
3788                        if (ret_val) {
3789                                printk(KERN_DEBUG "megasas: Failed to abort "
3790                                       "previous AEN command\n");
3791                                return ret_val;
3792                        }
3793                }
3794        }
3795
3796        cmd = megasas_get_cmd(instance);
3797
3798        if (!cmd)
3799                return -ENOMEM;
3800
3801        dcmd = &cmd->frame->dcmd;
3802
3803        memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3804
3805        /*
3806         * Prepare DCMD for aen registration
3807         */
3808        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3809
3810        dcmd->cmd = MFI_CMD_DCMD;
3811        dcmd->cmd_status = 0x0;
3812        dcmd->sge_count = 1;
3813        dcmd->flags = MFI_FRAME_DIR_READ;
3814        dcmd->timeout = 0;
3815        dcmd->pad_0 = 0;
3816        instance->last_seq_num = seq_num;
3817        dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3818        dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3819        dcmd->mbox.w[0] = seq_num;
3820        dcmd->mbox.w[1] = curr_aen.word;
3821        dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3822        dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3823
3824        if (instance->aen_cmd != NULL) {
3825                megasas_return_cmd(instance, cmd);
3826                return 0;
3827        }
3828
3829        /*
3830         * Store reference to the cmd used to register for AEN. When an
3831         * application wants us to register for AEN, we have to abort this
3832         * cmd and re-register with a new EVENT LOCALE supplied by that app
3833         */
3834        instance->aen_cmd = cmd;
3835
3836        /*
3837         * Issue the aen registration frame
3838         */
3839        instance->instancet->issue_dcmd(instance, cmd);
3840
3841        return 0;
3842}
3843
3844/**
3845 * megasas_start_aen -  Subscribes to AEN during driver load time
3846 * @instance:           Adapter soft state
3847 */
3848static int megasas_start_aen(struct megasas_instance *instance)
3849{
3850        struct megasas_evt_log_info eli;
3851        union megasas_evt_class_locale class_locale;
3852
3853        /*
3854         * Get the latest sequence number from FW
3855         */
3856        memset(&eli, 0, sizeof(eli));
3857
3858        if (megasas_get_seq_num(instance, &eli))
3859                return -1;
3860
3861        /*
3862         * Register AEN with FW for latest sequence number plus 1
3863         */
3864        class_locale.members.reserved = 0;
3865        class_locale.members.locale = MR_EVT_LOCALE_ALL;
3866        class_locale.members.class = MR_EVT_CLASS_DEBUG;
3867
3868        return megasas_register_aen(instance, eli.newest_seq_num + 1,
3869                                    class_locale.word);
3870}
3871
3872/**
3873 * megasas_io_attach -  Attaches this driver to SCSI mid-layer
3874 * @instance:           Adapter soft state
3875 */
3876static int megasas_io_attach(struct megasas_instance *instance)
3877{
3878        struct Scsi_Host *host = instance->host;
3879
3880        /*
3881         * Export parameters required by SCSI mid-layer
3882         */
3883        host->irq = instance->pdev->irq;
3884        host->unique_id = instance->unique_id;
3885        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3886                (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3887                host->can_queue =
3888                        instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3889        } else
3890                host->can_queue =
3891                        instance->max_fw_cmds - MEGASAS_INT_CMDS;
3892        host->this_id = instance->init_id;
3893        host->sg_tablesize = instance->max_num_sge;
3894
3895        if (instance->fw_support_ieee)
3896                instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3897
3898        /*
3899         * Check if the module parameter value for max_sectors can be used
3900         */
3901        if (max_sectors && max_sectors < instance->max_sectors_per_req)
3902                instance->max_sectors_per_req = max_sectors;
3903        else {
3904                if (max_sectors) {
3905                        if (((instance->pdev->device ==
3906                                PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3907                                (instance->pdev->device ==
3908                                PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3909                                (max_sectors <= MEGASAS_MAX_SECTORS)) {
3910                                instance->max_sectors_per_req = max_sectors;
3911                        } else {
3912                        printk(KERN_INFO "megasas: max_sectors should be > 0"
3913                                "and <= %d (or < 1MB for GEN2 controller)\n",
3914                                instance->max_sectors_per_req);
3915                        }
3916                }
3917        }
3918
3919        host->max_sectors = instance->max_sectors_per_req;
3920        host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3921        host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3922        host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3923        host->max_lun = MEGASAS_MAX_LUN;
3924        host->max_cmd_len = 16;
3925
3926        /* Fusion only supports host reset */
3927        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3928            (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3929                host->hostt->eh_device_reset_handler = NULL;
3930                host->hostt->eh_bus_reset_handler = NULL;
3931        }
3932
3933        /*
3934         * Notify the mid-layer about the new controller
3935         */
3936        if (scsi_add_host(host, &instance->pdev->dev)) {
3937                printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3938                return -ENODEV;
3939        }
3940
3941        /*
3942         * Trigger SCSI to scan our drives
3943         */
3944        scsi_scan_host(host);
3945        return 0;
3946}
3947
3948static int
3949megasas_set_dma_mask(struct pci_dev *pdev)
3950{
3951        /*
3952         * All our contollers are capable of performing 64-bit DMA
3953         */
3954        if (IS_DMA64) {
3955                if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3956
3957                        if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3958                                goto fail_set_dma_mask;
3959                }
3960        } else {
3961                if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3962                        goto fail_set_dma_mask;
3963        }
3964        return 0;
3965
3966fail_set_dma_mask:
3967        return 1;
3968}
3969
3970/**
3971 * megasas_probe_one -  PCI hotplug entry point
3972 * @pdev:               PCI device structure
3973 * @id:                 PCI ids of supported hotplugged adapter
3974 */
3975static int megasas_probe_one(struct pci_dev *pdev,
3976                             const struct pci_device_id *id)
3977{
3978        int rval, pos, i, j;
3979        struct Scsi_Host *host;
3980        struct megasas_instance *instance;
3981        u16 control = 0;
3982
3983        /* Reset MSI-X in the kdump kernel */
3984        if (reset_devices) {
3985                pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3986                if (pos) {
3987                        pci_read_config_word(pdev, msi_control_reg(pos),
3988                                             &control);
3989                        if (control & PCI_MSIX_FLAGS_ENABLE) {
3990                                dev_info(&pdev->dev, "resetting MSI-X\n");
3991                                pci_write_config_word(pdev,
3992                                                      msi_control_reg(pos),
3993                                                      control &
3994                                                      ~PCI_MSIX_FLAGS_ENABLE);
3995                        }
3996                }
3997        }
3998
3999        /*
4000         * Announce PCI information
4001         */
4002        printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4003               pdev->vendor, pdev->device, pdev->subsystem_vendor,
4004               pdev->subsystem_device);
4005
4006        printk("bus %d:slot %d:func %d\n",
4007               pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4008
4009        /*
4010         * PCI prepping: enable device set bus mastering and dma mask
4011         */
4012        rval = pci_enable_device_mem(pdev);
4013
4014        if (rval) {
4015                return rval;
4016        }
4017
4018        pci_set_master(pdev);
4019
4020        if (megasas_set_dma_mask(pdev))
4021                goto fail_set_dma_mask;
4022
4023        host = scsi_host_alloc(&megasas_template,
4024                               sizeof(struct megasas_instance));
4025
4026        if (!host) {
4027                printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4028                goto fail_alloc_instance;
4029        }
4030
4031        instance = (struct megasas_instance *)host->hostdata;
4032        memset(instance, 0, sizeof(*instance));
4033        atomic_set( &instance->fw_reset_no_pci_access, 0 );
4034        instance->pdev = pdev;
4035
4036        switch (instance->pdev->device) {
4037        case PCI_DEVICE_ID_LSI_FUSION:
4038        case PCI_DEVICE_ID_LSI_INVADER:
4039        {
4040                struct fusion_context *fusion;
4041
4042                instance->ctrl_context =
4043                        kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4044                if (!instance->ctrl_context) {
4045                        printk(KERN_DEBUG "megasas: Failed to allocate "
4046                               "memory for Fusion context info\n");
4047                        goto fail_alloc_dma_buf;
4048                }
4049                fusion = instance->ctrl_context;
4050                INIT_LIST_HEAD(&fusion->cmd_pool);
4051                spin_lock_init(&fusion->cmd_pool_lock);
4052        }
4053        break;
4054        default: /* For all other supported controllers */
4055
4056                instance->producer =
4057                        pci_alloc_consistent(pdev, sizeof(u32),
4058                                             &instance->producer_h);
4059                instance->consumer =
4060                        pci_alloc_consistent(pdev, sizeof(u32),
4061                                             &instance->consumer_h);
4062
4063                if (!instance->producer || !instance->consumer) {
4064                        printk(KERN_DEBUG "megasas: Failed to allocate"
4065                               "memory for producer, consumer\n");
4066                        goto fail_alloc_dma_buf;
4067                }
4068
4069                *instance->producer = 0;
4070                *instance->consumer = 0;
4071                break;
4072        }
4073
4074        megasas_poll_wait_aen = 0;
4075        instance->flag_ieee = 0;
4076        instance->ev = NULL;
4077        instance->issuepend_done = 1;
4078        instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4079        megasas_poll_wait_aen = 0;
4080
4081        instance->evt_detail = pci_alloc_consistent(pdev,
4082                                                    sizeof(struct
4083                                                           megasas_evt_detail),
4084                                                    &instance->evt_detail_h);
4085
4086        if (!instance->evt_detail) {
4087                printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4088                       "event detail structure\n");
4089                goto fail_alloc_dma_buf;
4090        }
4091
4092        /*
4093         * Initialize locks and queues
4094         */
4095        INIT_LIST_HEAD(&instance->cmd_pool);
4096        INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4097
4098        atomic_set(&instance->fw_outstanding,0);
4099
4100        init_waitqueue_head(&instance->int_cmd_wait_q);
4101        init_waitqueue_head(&instance->abort_cmd_wait_q);
4102
4103        spin_lock_init(&instance->cmd_pool_lock);
4104        spin_lock_init(&instance->hba_lock);
4105        spin_lock_init(&instance->completion_lock);
4106
4107        mutex_init(&instance->aen_mutex);
4108        mutex_init(&instance->reset_mutex);
4109
4110        /*
4111         * Initialize PCI related and misc parameters
4112         */
4113        instance->host = host;
4114        instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4115        instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4116
4117        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4118                (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4119                instance->flag_ieee = 1;
4120                sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4121        } else
4122                sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4123
4124        megasas_dbg_lvl = 0;
4125        instance->flag = 0;
4126        instance->unload = 1;
4127        instance->last_time = 0;
4128        instance->disableOnlineCtrlReset = 1;
4129
4130        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4131            (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4132                INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4133        else
4134                INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4135
4136        /*
4137         * Initialize MFI Firmware
4138         */
4139        if (megasas_init_fw(instance))
4140                goto fail_init_mfi;
4141
4142        /*
4143         * Register IRQ
4144         */
4145        if (instance->msix_vectors) {
4146                for (i = 0 ; i < instance->msix_vectors; i++) {
4147                        instance->irq_context[i].instance = instance;
4148                        instance->irq_context[i].MSIxIndex = i;
4149                        if (request_irq(instance->msixentry[i].vector,
4150                                        instance->instancet->service_isr, 0,
4151                                        "megasas",
4152                                        &instance->irq_context[i])) {
4153                                printk(KERN_DEBUG "megasas: Failed to "
4154                                       "register IRQ for vector %d.\n", i);
4155                                for (j = 0 ; j < i ; j++)
4156                                        free_irq(
4157                                                instance->msixentry[j].vector,
4158                                                &instance->irq_context[j]);
4159                                goto fail_irq;
4160                        }
4161                }
4162        } else {
4163                instance->irq_context[0].instance = instance;
4164                instance->irq_context[0].MSIxIndex = 0;
4165                if (request_irq(pdev->irq, instance->instancet->service_isr,
4166                                IRQF_SHARED, "megasas",
4167                                &instance->irq_context[0])) {
4168                        printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4169                        goto fail_irq;
4170                }
4171        }
4172
4173        instance->instancet->enable_intr(instance->reg_set);
4174
4175        /*
4176         * Store instance in PCI softstate
4177         */
4178        pci_set_drvdata(pdev, instance);
4179
4180        /*
4181         * Add this controller to megasas_mgmt_info structure so that it
4182         * can be exported to management applications
4183         */
4184        megasas_mgmt_info.count++;
4185        megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4186        megasas_mgmt_info.max_index++;
4187
4188        /*
4189         * Register with SCSI mid-layer
4190         */
4191        if (megasas_io_attach(instance))
4192                goto fail_io_attach;
4193
4194        instance->unload = 0;
4195
4196        /*
4197         * Initiate AEN (Asynchronous Event Notification)
4198         */
4199        if (megasas_start_aen(instance)) {
4200                printk(KERN_DEBUG "megasas: start aen failed\n");
4201                goto fail_start_aen;
4202        }
4203
4204        return 0;
4205
4206      fail_start_aen:
4207      fail_io_attach:
4208        megasas_mgmt_info.count--;
4209        megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4210        megasas_mgmt_info.max_index--;
4211
4212        pci_set_drvdata(pdev, NULL);
4213        instance->instancet->disable_intr(instance->reg_set);
4214        if (instance->msix_vectors)
4215                for (i = 0 ; i < instance->msix_vectors; i++)
4216                        free_irq(instance->msixentry[i].vector,
4217                                 &instance->irq_context[i]);
4218        else
4219                free_irq(instance->pdev->irq, &instance->irq_context[0]);
4220fail_irq:
4221        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4222            (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4223                megasas_release_fusion(instance);
4224        else
4225                megasas_release_mfi(instance);
4226      fail_init_mfi:
4227        if (instance->msix_vectors)
4228                pci_disable_msix(instance->pdev);
4229      fail_alloc_dma_buf:
4230        if (instance->evt_detail)
4231                pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4232                                    instance->evt_detail,
4233                                    instance->evt_detail_h);
4234
4235        if (instance->producer)
4236                pci_free_consistent(pdev, sizeof(u32), instance->producer,
4237                                    instance->producer_h);
4238        if (instance->consumer)
4239                pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4240                                    instance->consumer_h);
4241        scsi_host_put(host);
4242
4243      fail_alloc_instance:
4244      fail_set_dma_mask:
4245        pci_disable_device(pdev);
4246
4247        return -ENODEV;
4248}
4249
4250/**
4251 * megasas_flush_cache -        Requests FW to flush all its caches
4252 * @instance:                   Adapter soft state
4253 */
4254static void megasas_flush_cache(struct megasas_instance *instance)
4255{
4256        struct megasas_cmd *cmd;
4257        struct megasas_dcmd_frame *dcmd;
4258
4259        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4260                return;
4261
4262        cmd = megasas_get_cmd(instance);
4263
4264        if (!cmd)
4265                return;
4266
4267        dcmd = &cmd->frame->dcmd;
4268
4269        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4270
4271        dcmd->cmd = MFI_CMD_DCMD;
4272        dcmd->cmd_status = 0x0;
4273        dcmd->sge_count = 0;
4274        dcmd->flags = MFI_FRAME_DIR_NONE;
4275        dcmd->timeout = 0;
4276        dcmd->pad_0 = 0;
4277        dcmd->data_xfer_len = 0;
4278        dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4279        dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4280
4281        megasas_issue_blocked_cmd(instance, cmd);
4282
4283        megasas_return_cmd(instance, cmd);
4284
4285        return;
4286}
4287
4288/**
4289 * megasas_shutdown_controller -        Instructs FW to shutdown the controller
4290 * @instance:                           Adapter soft state
4291 * @opcode:                             Shutdown/Hibernate
4292 */
4293static void megasas_shutdown_controller(struct megasas_instance *instance,
4294                                        u32 opcode)
4295{
4296        struct megasas_cmd *cmd;
4297        struct megasas_dcmd_frame *dcmd;
4298
4299        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4300                return;
4301
4302        cmd = megasas_get_cmd(instance);
4303
4304        if (!cmd)
4305                return;
4306
4307        if (instance->aen_cmd)
4308                megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4309        if (instance->map_update_cmd)
4310                megasas_issue_blocked_abort_cmd(instance,
4311                                                instance->map_update_cmd);
4312        dcmd = &cmd->frame->dcmd;
4313
4314        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4315
4316        dcmd->cmd = MFI_CMD_DCMD;
4317        dcmd->cmd_status = 0x0;
4318        dcmd->sge_count = 0;
4319        dcmd->flags = MFI_FRAME_DIR_NONE;
4320        dcmd->timeout = 0;
4321        dcmd->pad_0 = 0;
4322        dcmd->data_xfer_len = 0;
4323        dcmd->opcode = opcode;
4324
4325        megasas_issue_blocked_cmd(instance, cmd);
4326
4327        megasas_return_cmd(instance, cmd);
4328
4329        return;
4330}
4331
4332#ifdef CONFIG_PM
4333/**
4334 * megasas_suspend -    driver suspend entry point
4335 * @pdev:               PCI device structure
4336 * @state:              PCI power state to suspend routine
4337 */
4338static int
4339megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4340{
4341        struct Scsi_Host *host;
4342        struct megasas_instance *instance;
4343        int i;
4344
4345        instance = pci_get_drvdata(pdev);
4346        host = instance->host;
4347        instance->unload = 1;
4348
4349        megasas_flush_cache(instance);
4350        megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4351
4352        /* cancel the delayed work if this work still in queue */
4353        if (instance->ev != NULL) {
4354                struct megasas_aen_event *ev = instance->ev;
4355                cancel_delayed_work_sync(&ev->hotplug_work);
4356                instance->ev = NULL;
4357        }
4358
4359        tasklet_kill(&instance->isr_tasklet);
4360
4361        pci_set_drvdata(instance->pdev, instance);
4362        instance->instancet->disable_intr(instance->reg_set);
4363
4364        if (instance->msix_vectors)
4365                for (i = 0 ; i < instance->msix_vectors; i++)
4366                        free_irq(instance->msixentry[i].vector,
4367                                 &instance->irq_context[i]);
4368        else
4369                free_irq(instance->pdev->irq, &instance->irq_context[0]);
4370        if (instance->msix_vectors)
4371                pci_disable_msix(instance->pdev);
4372
4373        pci_save_state(pdev);
4374        pci_disable_device(pdev);
4375
4376        pci_set_power_state(pdev, pci_choose_state(pdev, state));
4377
4378        return 0;
4379}
4380
4381/**
4382 * megasas_resume-      driver resume entry point
4383 * @pdev:               PCI device structure
4384 */
4385static int
4386megasas_resume(struct pci_dev *pdev)
4387{
4388        int rval, i, j;
4389        struct Scsi_Host *host;
4390        struct megasas_instance *instance;
4391
4392        instance = pci_get_drvdata(pdev);
4393        host = instance->host;
4394        pci_set_power_state(pdev, PCI_D0);
4395        pci_enable_wake(pdev, PCI_D0, 0);
4396        pci_restore_state(pdev);
4397
4398        /*
4399         * PCI prepping: enable device set bus mastering and dma mask
4400         */
4401        rval = pci_enable_device_mem(pdev);
4402
4403        if (rval) {
4404                printk(KERN_ERR "megasas: Enable device failed\n");
4405                return rval;
4406        }
4407
4408        pci_set_master(pdev);
4409
4410        if (megasas_set_dma_mask(pdev))
4411                goto fail_set_dma_mask;
4412
4413        /*
4414         * Initialize MFI Firmware
4415         */
4416
4417        atomic_set(&instance->fw_outstanding, 0);
4418
4419        /*
4420         * We expect the FW state to be READY
4421         */
4422        if (megasas_transition_to_ready(instance, 0))
4423                goto fail_ready_state;
4424
4425        /* Now re-enable MSI-X */
4426        if (instance->msix_vectors)
4427                pci_enable_msix(instance->pdev, instance->msixentry,
4428                                instance->msix_vectors);
4429
4430        switch (instance->pdev->device) {
4431        case PCI_DEVICE_ID_LSI_FUSION:
4432        case PCI_DEVICE_ID_LSI_INVADER:
4433        {
4434                megasas_reset_reply_desc(instance);
4435                if (megasas_ioc_init_fusion(instance)) {
4436                        megasas_free_cmds(instance);
4437                        megasas_free_cmds_fusion(instance);
4438                        goto fail_init_mfi;
4439                }
4440                if (!megasas_get_map_info(instance))
4441                        megasas_sync_map_info(instance);
4442        }
4443        break;
4444        default:
4445                *instance->producer = 0;
4446                *instance->consumer = 0;
4447                if (megasas_issue_init_mfi(instance))
4448                        goto fail_init_mfi;
4449                break;
4450        }
4451
4452        tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4453                     (unsigned long)instance);
4454
4455        /*
4456         * Register IRQ
4457         */
4458        if (instance->msix_vectors) {
4459                for (i = 0 ; i < instance->msix_vectors; i++) {
4460                        instance->irq_context[i].instance = instance;
4461                        instance->irq_context[i].MSIxIndex = i;
4462                        if (request_irq(instance->msixentry[i].vector,
4463                                        instance->instancet->service_isr, 0,
4464                                        "megasas",
4465                                        &instance->irq_context[i])) {
4466                                printk(KERN_DEBUG "megasas: Failed to "
4467                                       "register IRQ for vector %d.\n", i);
4468                                for (j = 0 ; j < i ; j++)
4469                                        free_irq(
4470                                                instance->msixentry[j].vector,
4471                                                &instance->irq_context[j]);
4472                                goto fail_irq;
4473                        }
4474                }
4475        } else {
4476                instance->irq_context[0].instance = instance;
4477                instance->irq_context[0].MSIxIndex = 0;
4478                if (request_irq(pdev->irq, instance->instancet->service_isr,
4479                                IRQF_SHARED, "megasas",
4480                                &instance->irq_context[0])) {
4481                        printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4482                        goto fail_irq;
4483                }
4484        }
4485
4486        instance->instancet->enable_intr(instance->reg_set);
4487        instance->unload = 0;
4488
4489        /*
4490         * Initiate AEN (Asynchronous Event Notification)
4491         */
4492        if (megasas_start_aen(instance))
4493                printk(KERN_ERR "megasas: Start AEN failed\n");
4494
4495        return 0;
4496
4497fail_irq:
4498fail_init_mfi:
4499        if (instance->evt_detail)
4500                pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4501                                instance->evt_detail,
4502                                instance->evt_detail_h);
4503
4504        if (instance->producer)
4505                pci_free_consistent(pdev, sizeof(u32), instance->producer,
4506                                instance->producer_h);
4507        if (instance->consumer)
4508                pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4509                                instance->consumer_h);
4510        scsi_host_put(host);
4511
4512fail_set_dma_mask:
4513fail_ready_state:
4514
4515        pci_disable_device(pdev);
4516
4517        return -ENODEV;
4518}
4519#else
4520#define megasas_suspend NULL
4521#define megasas_resume  NULL
4522#endif
4523
4524/**
4525 * megasas_detach_one - PCI hot"un"plug entry point
4526 * @pdev:               PCI device structure
4527 */
4528static void megasas_detach_one(struct pci_dev *pdev)
4529{
4530        int i;
4531        struct Scsi_Host *host;
4532        struct megasas_instance *instance;
4533        struct fusion_context *fusion;
4534
4535        instance = pci_get_drvdata(pdev);
4536        instance->unload = 1;
4537        host = instance->host;
4538        fusion = instance->ctrl_context;
4539
4540        scsi_remove_host(instance->host);
4541        megasas_flush_cache(instance);
4542        megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4543
4544        /* cancel the delayed work if this work still in queue*/
4545        if (instance->ev != NULL) {
4546                struct megasas_aen_event *ev = instance->ev;
4547                cancel_delayed_work_sync(&ev->hotplug_work);
4548                instance->ev = NULL;
4549        }
4550
4551        tasklet_kill(&instance->isr_tasklet);
4552
4553        /*
4554         * Take the instance off the instance array. Note that we will not
4555         * decrement the max_index. We let this array be sparse array
4556         */
4557        for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4558                if (megasas_mgmt_info.instance[i] == instance) {
4559                        megasas_mgmt_info.count--;
4560                        megasas_mgmt_info.instance[i] = NULL;
4561
4562                        break;
4563                }
4564        }
4565
4566        pci_set_drvdata(instance->pdev, NULL);
4567
4568        instance->instancet->disable_intr(instance->reg_set);
4569
4570        if (instance->msix_vectors)
4571                for (i = 0 ; i < instance->msix_vectors; i++)
4572                        free_irq(instance->msixentry[i].vector,
4573                                 &instance->irq_context[i]);
4574        else
4575                free_irq(instance->pdev->irq, &instance->irq_context[0]);
4576        if (instance->msix_vectors)
4577                pci_disable_msix(instance->pdev);
4578
4579        switch (instance->pdev->device) {
4580        case PCI_DEVICE_ID_LSI_FUSION:
4581        case PCI_DEVICE_ID_LSI_INVADER:
4582                megasas_release_fusion(instance);
4583                for (i = 0; i < 2 ; i++)
4584                        if (fusion->ld_map[i])
4585                                dma_free_coherent(&instance->pdev->dev,
4586                                                  fusion->map_sz,
4587                                                  fusion->ld_map[i],
4588                                                  fusion->
4589                                                  ld_map_phys[i]);
4590                kfree(instance->ctrl_context);
4591                break;
4592        default:
4593                megasas_release_mfi(instance);
4594                pci_free_consistent(pdev,
4595                                    sizeof(struct megasas_evt_detail),
4596                                    instance->evt_detail,
4597                                    instance->evt_detail_h);
4598                pci_free_consistent(pdev, sizeof(u32),
4599                                    instance->producer,
4600                                    instance->producer_h);
4601                pci_free_consistent(pdev, sizeof(u32),
4602                                    instance->consumer,
4603                                    instance->consumer_h);
4604                break;
4605        }
4606
4607        scsi_host_put(host);
4608
4609        pci_set_drvdata(pdev, NULL);
4610
4611        pci_disable_device(pdev);
4612
4613        return;
4614}
4615
4616/**
4617 * megasas_shutdown -   Shutdown entry point
4618 * @device:             Generic device structure
4619 */
4620static void megasas_shutdown(struct pci_dev *pdev)
4621{
4622        int i;
4623        struct megasas_instance *instance = pci_get_drvdata(pdev);
4624
4625        instance->unload = 1;
4626        megasas_flush_cache(instance);
4627        megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4628        instance->instancet->disable_intr(instance->reg_set);
4629        if (instance->msix_vectors)
4630                for (i = 0 ; i < instance->msix_vectors; i++)
4631                        free_irq(instance->msixentry[i].vector,
4632                                 &instance->irq_context[i]);
4633        else
4634                free_irq(instance->pdev->irq, &instance->irq_context[0]);
4635        if (instance->msix_vectors)
4636                pci_disable_msix(instance->pdev);
4637}
4638
4639/**
4640 * megasas_mgmt_open -  char node "open" entry point
4641 */
4642static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4643{
4644        /*
4645         * Allow only those users with admin rights
4646         */
4647        if (!capable(CAP_SYS_ADMIN))
4648                return -EACCES;
4649
4650        return 0;
4651}
4652
4653/**
4654 * megasas_mgmt_fasync -        Async notifier registration from applications
4655 *
4656 * This function adds the calling process to a driver global queue. When an
4657 * event occurs, SIGIO will be sent to all processes in this queue.
4658 */
4659static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4660{
4661        int rc;
4662
4663        mutex_lock(&megasas_async_queue_mutex);
4664
4665        rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4666
4667        mutex_unlock(&megasas_async_queue_mutex);
4668
4669        if (rc >= 0) {
4670                /* For sanity check when we get ioctl */
4671                filep->private_data = filep;
4672                return 0;
4673        }
4674
4675        printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4676
4677        return rc;
4678}
4679
4680/**
4681 * megasas_mgmt_poll -  char node "poll" entry point
4682 * */
4683static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4684{
4685        unsigned int mask;
4686        unsigned long flags;
4687        poll_wait(file, &megasas_poll_wait, wait);
4688        spin_lock_irqsave(&poll_aen_lock, flags);
4689        if (megasas_poll_wait_aen)
4690                mask =   (POLLIN | POLLRDNORM);
4691        else
4692                mask = 0;
4693        spin_unlock_irqrestore(&poll_aen_lock, flags);
4694        return mask;
4695}
4696
4697/**
4698 * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
4699 * @instance:                   Adapter soft state
4700 * @argp:                       User's ioctl packet
4701 */
4702static int
4703megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4704                      struct megasas_iocpacket __user * user_ioc,
4705                      struct megasas_iocpacket *ioc)
4706{
4707        struct megasas_sge32 *kern_sge32;
4708        struct megasas_cmd *cmd;
4709        void *kbuff_arr[MAX_IOCTL_SGE];
4710        dma_addr_t buf_handle = 0;
4711        int error = 0, i;
4712        void *sense = NULL;
4713        dma_addr_t sense_handle;
4714        unsigned long *sense_ptr;
4715
4716        memset(kbuff_arr, 0, sizeof(kbuff_arr));
4717
4718        if (ioc->sge_count > MAX_IOCTL_SGE) {
4719                printk(KERN_DEBUG "megasas: SGE count [%d] >  max limit [%d]\n",
4720                       ioc->sge_count, MAX_IOCTL_SGE);
4721                return -EINVAL;
4722        }
4723
4724        cmd = megasas_get_cmd(instance);
4725        if (!cmd) {
4726                printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4727                return -ENOMEM;
4728        }
4729
4730        /*
4731         * User's IOCTL packet has 2 frames (maximum). Copy those two
4732         * frames into our cmd's frames. cmd->frame's context will get
4733         * overwritten when we copy from user's frames. So set that value
4734         * alone separately
4735         */
4736        memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4737        cmd->frame->hdr.context = cmd->index;
4738        cmd->frame->hdr.pad_0 = 0;
4739        cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4740                                   MFI_FRAME_SENSE64);
4741
4742        /*
4743         * The management interface between applications and the fw uses
4744         * MFI frames. E.g, RAID configuration changes, LD property changes
4745         * etc are accomplishes through different kinds of MFI frames. The
4746         * driver needs to care only about substituting user buffers with
4747         * kernel buffers in SGLs. The location of SGL is embedded in the
4748         * struct iocpacket itself.
4749         */
4750        kern_sge32 = (struct megasas_sge32 *)
4751            ((unsigned long)cmd->frame + ioc->sgl_off);
4752
4753        /*
4754         * For each user buffer, create a mirror buffer and copy in
4755         */
4756        for (i = 0; i < ioc->sge_count; i++) {
4757                if (!ioc->sgl[i].iov_len)
4758                        continue;
4759
4760                kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4761                                                    ioc->sgl[i].iov_len,
4762                                                    &buf_handle, GFP_KERNEL);
4763                if (!kbuff_arr[i]) {
4764                        printk(KERN_DEBUG "megasas: Failed to alloc "
4765                               "kernel SGL buffer for IOCTL \n");
4766                        error = -ENOMEM;
4767                        goto out;
4768                }
4769
4770                /*
4771                 * We don't change the dma_coherent_mask, so
4772                 * pci_alloc_consistent only returns 32bit addresses
4773                 */
4774                kern_sge32[i].phys_addr = (u32) buf_handle;
4775                kern_sge32[i].length = ioc->sgl[i].iov_len;
4776
4777                /*
4778                 * We created a kernel buffer corresponding to the
4779                 * user buffer. Now copy in from the user buffer
4780                 */
4781                if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4782                                   (u32) (ioc->sgl[i].iov_len))) {
4783                        error = -EFAULT;
4784                        goto out;
4785                }
4786        }
4787
4788        if (ioc->sense_len) {
4789                sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4790                                             &sense_handle, GFP_KERNEL);
4791                if (!sense) {
4792                        error = -ENOMEM;
4793                        goto out;
4794                }
4795
4796                sense_ptr =
4797                (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4798                *sense_ptr = sense_handle;
4799        }
4800
4801        /*
4802         * Set the sync_cmd flag so that the ISR knows not to complete this
4803         * cmd to the SCSI mid-layer
4804         */
4805        cmd->sync_cmd = 1;
4806        megasas_issue_blocked_cmd(instance, cmd);
4807        cmd->sync_cmd = 0;
4808
4809        /*
4810         * copy out the kernel buffers to user buffers
4811         */
4812        for (i = 0; i < ioc->sge_count; i++) {
4813                if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4814                                 ioc->sgl[i].iov_len)) {
4815                        error = -EFAULT;
4816                        goto out;
4817                }
4818        }
4819
4820        /*
4821         * copy out the sense
4822         */
4823        if (ioc->sense_len) {
4824                /*
4825                 * sense_ptr points to the location that has the user
4826                 * sense buffer address
4827                 */
4828                sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4829                                ioc->sense_off);
4830
4831                if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4832                                 sense, ioc->sense_len)) {
4833                        printk(KERN_ERR "megasas: Failed to copy out to user "
4834                                        "sense data\n");
4835                        error = -EFAULT;
4836                        goto out;
4837                }
4838        }
4839
4840        /*
4841         * copy the status codes returned by the fw
4842         */
4843        if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4844                         &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4845                printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4846                error = -EFAULT;
4847        }
4848
4849      out:
4850        if (sense) {
4851                dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4852                                    sense, sense_handle);
4853        }
4854
4855        for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4856                dma_free_coherent(&instance->pdev->dev,
4857                                    kern_sge32[i].length,
4858                                    kbuff_arr[i], kern_sge32[i].phys_addr);
4859        }
4860
4861        megasas_return_cmd(instance, cmd);
4862        return error;
4863}
4864
4865static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4866{
4867        struct megasas_iocpacket __user *user_ioc =
4868            (struct megasas_iocpacket __user *)arg;
4869        struct megasas_iocpacket *ioc;
4870        struct megasas_instance *instance;
4871        int error;
4872        int i;
4873        unsigned long flags;
4874        u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4875
4876        ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4877        if (!ioc)
4878                return -ENOMEM;
4879
4880        if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4881                error = -EFAULT;
4882                goto out_kfree_ioc;
4883        }
4884
4885        instance = megasas_lookup_instance(ioc->host_no);
4886        if (!instance) {
4887                error = -ENODEV;
4888                goto out_kfree_ioc;