linux/drivers/scsi/megaraid/megaraid_sas_fusion.c
<<
>>
Prefs
   1/*
   2 *  Linux MegaRAID driver for SAS based RAID controllers
   3 *
   4 *  Copyright (c) 2009-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_fusion.c
  21 *
  22 *  Authors: LSI Corporation
  23 *           Sumant Patro
  24 *           Adam Radford <linuxraid@lsi.com>
  25 *
  26 *  Send feedback to: <megaraidlinux@lsi.com>
  27 *
  28 *  Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  29 *     ATTN: Linuxraid
  30 */
  31
  32#include <linux/kernel.h>
  33#include <linux/types.h>
  34#include <linux/pci.h>
  35#include <linux/list.h>
  36#include <linux/moduleparam.h>
  37#include <linux/module.h>
  38#include <linux/spinlock.h>
  39#include <linux/interrupt.h>
  40#include <linux/delay.h>
  41#include <linux/uio.h>
  42#include <linux/uaccess.h>
  43#include <linux/fs.h>
  44#include <linux/compat.h>
  45#include <linux/blkdev.h>
  46#include <linux/mutex.h>
  47#include <linux/poll.h>
  48
  49#include <scsi/scsi.h>
  50#include <scsi/scsi_cmnd.h>
  51#include <scsi/scsi_device.h>
  52#include <scsi/scsi_host.h>
  53
  54#include "megaraid_sas_fusion.h"
  55#include "megaraid_sas.h"
  56
  57extern void megasas_free_cmds(struct megasas_instance *instance);
  58extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  59                                           *instance);
  60extern void
  61megasas_complete_cmd(struct megasas_instance *instance,
  62                     struct megasas_cmd *cmd, u8 alt_status);
  63int megasas_is_ldio(struct scsi_cmnd *cmd);
  64int
  65wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
  66
  67void
  68megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  69int megasas_alloc_cmds(struct megasas_instance *instance);
  70int
  71megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  72int
  73megasas_issue_polled(struct megasas_instance *instance,
  74                     struct megasas_cmd *cmd);
  75void
  76megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  77
  78u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
  79                           struct IO_REQUEST_INFO *in_info);
  80int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  81void megaraid_sas_kill_hba(struct megasas_instance *instance);
  82
  83extern u32 megasas_dbg_lvl;
  84extern int resetwaittime;
  85
  86/**
  87 * megasas_enable_intr_fusion - Enables interrupts
  88 * @regs:                       MFI register set
  89 */
  90void
  91megasas_enable_intr_fusion(struct megasas_instance *instance)
  92{
  93        struct megasas_register_set __iomem *regs;
  94        regs = instance->reg_set;
  95        /* For Thunderbolt/Invader also clear intr on enable */
  96        writel(~0, &regs->outbound_intr_status);
  97        readl(&regs->outbound_intr_status);
  98
  99        writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 100
 101        /* Dummy readl to force pci flush */
 102        readl(&regs->outbound_intr_mask);
 103        instance->mask_interrupts = 0;
 104}
 105
 106/**
 107 * megasas_disable_intr_fusion - Disables interrupt
 108 * @regs:                        MFI register set
 109 */
 110void
 111megasas_disable_intr_fusion(struct megasas_instance *instance)
 112{
 113        u32 mask = 0xFFFFFFFF;
 114        u32 status;
 115        struct megasas_register_set __iomem *regs;
 116        regs = instance->reg_set;
 117        instance->mask_interrupts = 1;
 118
 119        writel(mask, &regs->outbound_intr_mask);
 120        /* Dummy readl to force pci flush */
 121        status = readl(&regs->outbound_intr_mask);
 122}
 123
 124int
 125megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
 126{
 127        u32 status;
 128        /*
 129         * Check if it is our interrupt
 130         */
 131        status = readl(&regs->outbound_intr_status);
 132
 133        if (status & 1) {
 134                writel(status, &regs->outbound_intr_status);
 135                readl(&regs->outbound_intr_status);
 136                return 1;
 137        }
 138        if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
 139                return 0;
 140
 141        return 1;
 142}
 143
 144/**
 145 * megasas_get_cmd_fusion -     Get a command from the free pool
 146 * @instance:           Adapter soft state
 147 *
 148 * Returns a free command from the pool
 149 */
 150struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
 151                                                  *instance)
 152{
 153        unsigned long flags;
 154        struct fusion_context *fusion =
 155                (struct fusion_context *)instance->ctrl_context;
 156        struct megasas_cmd_fusion *cmd = NULL;
 157
 158        spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
 159
 160        if (!list_empty(&fusion->cmd_pool)) {
 161                cmd = list_entry((&fusion->cmd_pool)->next,
 162                                 struct megasas_cmd_fusion, list);
 163                list_del_init(&cmd->list);
 164        } else {
 165                printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
 166        }
 167
 168        spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
 169        return cmd;
 170}
 171
 172/**
 173 * megasas_return_cmd_fusion -  Return a cmd to free command pool
 174 * @instance:           Adapter soft state
 175 * @cmd:                Command packet to be returned to free command pool
 176 */
 177static inline void
 178megasas_return_cmd_fusion(struct megasas_instance *instance,
 179                          struct megasas_cmd_fusion *cmd)
 180{
 181        unsigned long flags;
 182        struct fusion_context *fusion =
 183                (struct fusion_context *)instance->ctrl_context;
 184
 185        spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
 186
 187        cmd->scmd = NULL;
 188        cmd->sync_cmd_idx = (u32)ULONG_MAX;
 189        list_add_tail(&cmd->list, &fusion->cmd_pool);
 190
 191        spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
 192}
 193
 194/**
 195 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
 196 * @instance:                           Adapter soft state
 197 */
 198static void megasas_teardown_frame_pool_fusion(
 199        struct megasas_instance *instance)
 200{
 201        int i;
 202        struct fusion_context *fusion = instance->ctrl_context;
 203
 204        u16 max_cmd = instance->max_fw_cmds;
 205
 206        struct megasas_cmd_fusion *cmd;
 207
 208        if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
 209                printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
 210                       "sense pool : %p\n", fusion->sg_dma_pool,
 211                       fusion->sense_dma_pool);
 212                return;
 213        }
 214
 215        /*
 216         * Return all frames to pool
 217         */
 218        for (i = 0; i < max_cmd; i++) {
 219
 220                cmd = fusion->cmd_list[i];
 221
 222                if (cmd->sg_frame)
 223                        pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
 224                                      cmd->sg_frame_phys_addr);
 225
 226                if (cmd->sense)
 227                        pci_pool_free(fusion->sense_dma_pool, cmd->sense,
 228                                      cmd->sense_phys_addr);
 229        }
 230
 231        /*
 232         * Now destroy the pool itself
 233         */
 234        pci_pool_destroy(fusion->sg_dma_pool);
 235        pci_pool_destroy(fusion->sense_dma_pool);
 236
 237        fusion->sg_dma_pool = NULL;
 238        fusion->sense_dma_pool = NULL;
 239}
 240
 241/**
 242 * megasas_free_cmds_fusion -   Free all the cmds in the free cmd pool
 243 * @instance:           Adapter soft state
 244 */
 245void
 246megasas_free_cmds_fusion(struct megasas_instance *instance)
 247{
 248        int i;
 249        struct fusion_context *fusion = instance->ctrl_context;
 250
 251        u32 max_cmds, req_sz, reply_sz, io_frames_sz;
 252
 253
 254        req_sz = fusion->request_alloc_sz;
 255        reply_sz = fusion->reply_alloc_sz;
 256        io_frames_sz = fusion->io_frames_alloc_sz;
 257
 258        max_cmds = instance->max_fw_cmds;
 259
 260        /* Free descriptors and request Frames memory */
 261        if (fusion->req_frames_desc)
 262                dma_free_coherent(&instance->pdev->dev, req_sz,
 263                                  fusion->req_frames_desc,
 264                                  fusion->req_frames_desc_phys);
 265
 266        if (fusion->reply_frames_desc) {
 267                pci_pool_free(fusion->reply_frames_desc_pool,
 268                              fusion->reply_frames_desc,
 269                              fusion->reply_frames_desc_phys);
 270                pci_pool_destroy(fusion->reply_frames_desc_pool);
 271        }
 272
 273        if (fusion->io_request_frames) {
 274                pci_pool_free(fusion->io_request_frames_pool,
 275                              fusion->io_request_frames,
 276                              fusion->io_request_frames_phys);
 277                pci_pool_destroy(fusion->io_request_frames_pool);
 278        }
 279
 280        /* Free the Fusion frame pool */
 281        megasas_teardown_frame_pool_fusion(instance);
 282
 283        /* Free all the commands in the cmd_list */
 284        for (i = 0; i < max_cmds; i++)
 285                kfree(fusion->cmd_list[i]);
 286
 287        /* Free the cmd_list buffer itself */
 288        kfree(fusion->cmd_list);
 289        fusion->cmd_list = NULL;
 290
 291        INIT_LIST_HEAD(&fusion->cmd_pool);
 292}
 293
 294/**
 295 * megasas_create_frame_pool_fusion -   Creates DMA pool for cmd frames
 296 * @instance:                   Adapter soft state
 297 *
 298 */
 299static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
 300{
 301        int i;
 302        u32 max_cmd;
 303        struct fusion_context *fusion;
 304        struct megasas_cmd_fusion *cmd;
 305        u32 total_sz_chain_frame;
 306
 307        fusion = instance->ctrl_context;
 308        max_cmd = instance->max_fw_cmds;
 309
 310        total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
 311
 312        /*
 313         * Use DMA pool facility provided by PCI layer
 314         */
 315
 316        fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
 317                                              instance->pdev,
 318                                              total_sz_chain_frame, 4,
 319                                              0);
 320        if (!fusion->sg_dma_pool) {
 321                printk(KERN_DEBUG "megasas: failed to setup request pool "
 322                       "fusion\n");
 323                return -ENOMEM;
 324        }
 325        fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
 326                                                 instance->pdev,
 327                                                 SCSI_SENSE_BUFFERSIZE, 64, 0);
 328
 329        if (!fusion->sense_dma_pool) {
 330                printk(KERN_DEBUG "megasas: failed to setup sense pool "
 331                       "fusion\n");
 332                pci_pool_destroy(fusion->sg_dma_pool);
 333                fusion->sg_dma_pool = NULL;
 334                return -ENOMEM;
 335        }
 336
 337        /*
 338         * Allocate and attach a frame to each of the commands in cmd_list
 339         */
 340        for (i = 0; i < max_cmd; i++) {
 341
 342                cmd = fusion->cmd_list[i];
 343
 344                cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
 345                                               GFP_KERNEL,
 346                                               &cmd->sg_frame_phys_addr);
 347
 348                cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
 349                                            GFP_KERNEL, &cmd->sense_phys_addr);
 350                /*
 351                 * megasas_teardown_frame_pool_fusion() takes care of freeing
 352                 * whatever has been allocated
 353                 */
 354                if (!cmd->sg_frame || !cmd->sense) {
 355                        printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
 356                        megasas_teardown_frame_pool_fusion(instance);
 357                        return -ENOMEM;
 358                }
 359        }
 360        return 0;
 361}
 362
 363/**
 364 * megasas_alloc_cmds_fusion -  Allocates the command packets
 365 * @instance:           Adapter soft state
 366 *
 367 *
 368 * Each frame has a 32-bit field called context. This context is used to get
 369 * back the megasas_cmd_fusion from the frame when a frame gets completed
 370 * In this driver, the 32 bit values are the indices into an array cmd_list.
 371 * This array is used only to look up the megasas_cmd_fusion given the context.
 372 * The free commands themselves are maintained in a linked list called cmd_pool.
 373 *
 374 * cmds are formed in the io_request and sg_frame members of the
 375 * megasas_cmd_fusion. The context field is used to get a request descriptor
 376 * and is used as SMID of the cmd.
 377 * SMID value range is from 1 to max_fw_cmds.
 378 */
 379int
 380megasas_alloc_cmds_fusion(struct megasas_instance *instance)
 381{
 382        int i, j, count;
 383        u32 max_cmd, io_frames_sz;
 384        struct fusion_context *fusion;
 385        struct megasas_cmd_fusion *cmd;
 386        union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
 387        u32 offset;
 388        dma_addr_t io_req_base_phys;
 389        u8 *io_req_base;
 390
 391        fusion = instance->ctrl_context;
 392
 393        max_cmd = instance->max_fw_cmds;
 394
 395        fusion->req_frames_desc =
 396                dma_alloc_coherent(&instance->pdev->dev,
 397                                   fusion->request_alloc_sz,
 398                                   &fusion->req_frames_desc_phys, GFP_KERNEL);
 399
 400        if (!fusion->req_frames_desc) {
 401                printk(KERN_ERR "megasas; Could not allocate memory for "
 402                       "request_frames\n");
 403                goto fail_req_desc;
 404        }
 405
 406        count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
 407        fusion->reply_frames_desc_pool =
 408                pci_pool_create("reply_frames pool", instance->pdev,
 409                                fusion->reply_alloc_sz * count, 16, 0);
 410
 411        if (!fusion->reply_frames_desc_pool) {
 412                printk(KERN_ERR "megasas; Could not allocate memory for "
 413                       "reply_frame pool\n");
 414                goto fail_reply_desc;
 415        }
 416
 417        fusion->reply_frames_desc =
 418                pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
 419                               &fusion->reply_frames_desc_phys);
 420        if (!fusion->reply_frames_desc) {
 421                printk(KERN_ERR "megasas; Could not allocate memory for "
 422                       "reply_frame pool\n");
 423                pci_pool_destroy(fusion->reply_frames_desc_pool);
 424                goto fail_reply_desc;
 425        }
 426
 427        reply_desc = fusion->reply_frames_desc;
 428        for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
 429                reply_desc->Words = ULLONG_MAX;
 430
 431        io_frames_sz = fusion->io_frames_alloc_sz;
 432
 433        fusion->io_request_frames_pool =
 434                pci_pool_create("io_request_frames pool", instance->pdev,
 435                                fusion->io_frames_alloc_sz, 16, 0);
 436
 437        if (!fusion->io_request_frames_pool) {
 438                printk(KERN_ERR "megasas: Could not allocate memory for "
 439                       "io_request_frame pool\n");
 440                goto fail_io_frames;
 441        }
 442
 443        fusion->io_request_frames =
 444                pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
 445                               &fusion->io_request_frames_phys);
 446        if (!fusion->io_request_frames) {
 447                printk(KERN_ERR "megasas: Could not allocate memory for "
 448                       "io_request_frames frames\n");
 449                pci_pool_destroy(fusion->io_request_frames_pool);
 450                goto fail_io_frames;
 451        }
 452
 453        /*
 454         * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
 455         * Allocate the dynamic array first and then allocate individual
 456         * commands.
 457         */
 458        fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
 459                                   * max_cmd, GFP_KERNEL);
 460
 461        if (!fusion->cmd_list) {
 462                printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
 463                       "memory for cmd_list_fusion\n");
 464                goto fail_cmd_list;
 465        }
 466
 467        max_cmd = instance->max_fw_cmds;
 468        for (i = 0; i < max_cmd; i++) {
 469                fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
 470                                              GFP_KERNEL);
 471                if (!fusion->cmd_list[i]) {
 472                        printk(KERN_ERR "Could not alloc cmd list fusion\n");
 473
 474                        for (j = 0; j < i; j++)
 475                                kfree(fusion->cmd_list[j]);
 476
 477                        kfree(fusion->cmd_list);
 478                        fusion->cmd_list = NULL;
 479                        goto fail_cmd_list;
 480                }
 481        }
 482
 483        /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
 484        io_req_base = fusion->io_request_frames +
 485                MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
 486        io_req_base_phys = fusion->io_request_frames_phys +
 487                MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
 488
 489        /*
 490         * Add all the commands to command pool (fusion->cmd_pool)
 491         */
 492
 493        /* SMID 0 is reserved. Set SMID/index from 1 */
 494        for (i = 0; i < max_cmd; i++) {
 495                cmd = fusion->cmd_list[i];
 496                offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
 497                memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
 498                cmd->index = i + 1;
 499                cmd->scmd = NULL;
 500                cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
 501                cmd->instance = instance;
 502                cmd->io_request =
 503                        (struct MPI2_RAID_SCSI_IO_REQUEST *)
 504                  (io_req_base + offset);
 505                memset(cmd->io_request, 0,
 506                       sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
 507                cmd->io_request_phys_addr = io_req_base_phys + offset;
 508
 509                list_add_tail(&cmd->list, &fusion->cmd_pool);
 510        }
 511
 512        /*
 513         * Create a frame pool and assign one frame to each cmd
 514         */
 515        if (megasas_create_frame_pool_fusion(instance)) {
 516                printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
 517                megasas_free_cmds_fusion(instance);
 518                goto fail_req_desc;
 519        }
 520
 521        return 0;
 522
 523fail_cmd_list:
 524        pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
 525                      fusion->io_request_frames_phys);
 526        pci_pool_destroy(fusion->io_request_frames_pool);
 527fail_io_frames:
 528        dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
 529                          fusion->reply_frames_desc,
 530                          fusion->reply_frames_desc_phys);
 531        pci_pool_free(fusion->reply_frames_desc_pool,
 532                      fusion->reply_frames_desc,
 533                      fusion->reply_frames_desc_phys);
 534        pci_pool_destroy(fusion->reply_frames_desc_pool);
 535
 536fail_reply_desc:
 537        dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
 538                          fusion->req_frames_desc,
 539                          fusion->req_frames_desc_phys);
 540fail_req_desc:
 541        return -ENOMEM;
 542}
 543
 544/**
 545 * wait_and_poll -      Issues a polling command
 546 * @instance:                   Adapter soft state
 547 * @cmd:                        Command packet to be issued
 548 *
 549 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
 550 */
 551int
 552wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
 553{
 554        int i;
 555        struct megasas_header *frame_hdr = &cmd->frame->hdr;
 556
 557        u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
 558
 559        /*
 560         * Wait for cmd_status to change
 561         */
 562        for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
 563                rmb();
 564                msleep(20);
 565        }
 566
 567        if (frame_hdr->cmd_status == 0xff)
 568                return -ETIME;
 569
 570        return 0;
 571}
 572
 573/**
 574 * megasas_ioc_init_fusion -    Initializes the FW
 575 * @instance:           Adapter soft state
 576 *
 577 * Issues the IOC Init cmd
 578 */
 579int
 580megasas_ioc_init_fusion(struct megasas_instance *instance)
 581{
 582        struct megasas_init_frame *init_frame;
 583        struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
 584        dma_addr_t      ioc_init_handle;
 585        struct megasas_cmd *cmd;
 586        u8 ret;
 587        struct fusion_context *fusion;
 588        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
 589        int i;
 590        struct megasas_header *frame_hdr;
 591
 592        fusion = instance->ctrl_context;
 593
 594        cmd = megasas_get_cmd(instance);
 595
 596        if (!cmd) {
 597                printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
 598                ret = 1;
 599                goto fail_get_cmd;
 600        }
 601
 602        IOCInitMessage =
 603          dma_alloc_coherent(&instance->pdev->dev,
 604                             sizeof(struct MPI2_IOC_INIT_REQUEST),
 605                             &ioc_init_handle, GFP_KERNEL);
 606
 607        if (!IOCInitMessage) {
 608                printk(KERN_ERR "Could not allocate memory for "
 609                       "IOCInitMessage\n");
 610                ret = 1;
 611                goto fail_fw_init;
 612        }
 613
 614        memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
 615
 616        IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
 617        IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
 618        IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
 619        IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
 620        IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
 621
 622        IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
 623        IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
 624        IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
 625        IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
 626        init_frame = (struct megasas_init_frame *)cmd->frame;
 627        memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
 628
 629        frame_hdr = &cmd->frame->hdr;
 630        frame_hdr->cmd_status = 0xFF;
 631        frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
 632
 633        init_frame->cmd = MFI_CMD_INIT;
 634        init_frame->cmd_status = 0xFF;
 635
 636        /* driver support Extended MSIX */
 637        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
 638                (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
 639                init_frame->driver_operations.
 640                        mfi_capabilities.support_additional_msix = 1;
 641        /* driver supports HA / Remote LUN over Fast Path interface */
 642        init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
 643                = 1;
 644        /* Convert capability to LE32 */
 645        cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
 646
 647        init_frame->queue_info_new_phys_addr_lo = cpu_to_le32((u32)ioc_init_handle);
 648        init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
 649
 650        req_desc =
 651          (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
 652
 653        req_desc->Words = 0;
 654        req_desc->MFAIo.RequestFlags =
 655                (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
 656                 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
 657        cpu_to_le32s((u32 *)&req_desc->MFAIo);
 658        req_desc->Words |= cpu_to_le64(cmd->frame_phys_addr);
 659
 660        /*
 661         * disable the intr before firing the init frame
 662         */
 663        instance->instancet->disable_intr(instance);
 664
 665        for (i = 0; i < (10 * 1000); i += 20) {
 666                if (readl(&instance->reg_set->doorbell) & 1)
 667                        msleep(20);
 668                else
 669                        break;
 670        }
 671
 672        instance->instancet->fire_cmd(instance, req_desc->u.low,
 673                                      req_desc->u.high, instance->reg_set);
 674
 675        wait_and_poll(instance, cmd);
 676
 677        frame_hdr = &cmd->frame->hdr;
 678        if (frame_hdr->cmd_status != 0) {
 679                ret = 1;
 680                goto fail_fw_init;
 681        }
 682        printk(KERN_ERR "megasas:IOC Init cmd success\n");
 683
 684        ret = 0;
 685
 686fail_fw_init:
 687        megasas_return_cmd(instance, cmd);
 688        if (IOCInitMessage)
 689                dma_free_coherent(&instance->pdev->dev,
 690                                  sizeof(struct MPI2_IOC_INIT_REQUEST),
 691                                  IOCInitMessage, ioc_init_handle);
 692fail_get_cmd:
 693        return ret;
 694}
 695
 696/*
 697 * megasas_get_ld_map_info -    Returns FW's ld_map structure
 698 * @instance:                           Adapter soft state
 699 * @pend:                               Pend the command or not
 700 * Issues an internal command (DCMD) to get the FW's controller PD
 701 * list structure.  This information is mainly used to find out SYSTEM
 702 * supported by the FW.
 703 */
 704static int
 705megasas_get_ld_map_info(struct megasas_instance *instance)
 706{
 707        int ret = 0;
 708        struct megasas_cmd *cmd;
 709        struct megasas_dcmd_frame *dcmd;
 710        struct MR_FW_RAID_MAP_ALL *ci;
 711        dma_addr_t ci_h = 0;
 712        u32 size_map_info;
 713        struct fusion_context *fusion;
 714
 715        cmd = megasas_get_cmd(instance);
 716
 717        if (!cmd) {
 718                printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
 719                return -ENOMEM;
 720        }
 721
 722        fusion = instance->ctrl_context;
 723
 724        if (!fusion) {
 725                megasas_return_cmd(instance, cmd);
 726                return 1;
 727        }
 728
 729        dcmd = &cmd->frame->dcmd;
 730
 731        size_map_info = sizeof(struct MR_FW_RAID_MAP) +
 732                (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
 733
 734        ci = fusion->ld_map[(instance->map_id & 1)];
 735        ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
 736
 737        if (!ci) {
 738                printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
 739                megasas_return_cmd(instance, cmd);
 740                return -ENOMEM;
 741        }
 742
 743        memset(ci, 0, sizeof(*ci));
 744        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
 745
 746        dcmd->cmd = MFI_CMD_DCMD;
 747        dcmd->cmd_status = 0xFF;
 748        dcmd->sge_count = 1;
 749        dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
 750        dcmd->timeout = 0;
 751        dcmd->pad_0 = 0;
 752        dcmd->data_xfer_len = cpu_to_le32(size_map_info);
 753        dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
 754        dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
 755        dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
 756
 757        if (!megasas_issue_polled(instance, cmd))
 758                ret = 0;
 759        else {
 760                printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
 761                ret = -1;
 762        }
 763
 764        megasas_return_cmd(instance, cmd);
 765
 766        return ret;
 767}
 768
 769u8
 770megasas_get_map_info(struct megasas_instance *instance)
 771{
 772        struct fusion_context *fusion = instance->ctrl_context;
 773
 774        fusion->fast_path_io = 0;
 775        if (!megasas_get_ld_map_info(instance)) {
 776                if (MR_ValidateMapInfo(instance)) {
 777                        fusion->fast_path_io = 1;
 778                        return 0;
 779                }
 780        }
 781        return 1;
 782}
 783
 784/*
 785 * megasas_sync_map_info -      Returns FW's ld_map structure
 786 * @instance:                           Adapter soft state
 787 *
 788 * Issues an internal command (DCMD) to get the FW's controller PD
 789 * list structure.  This information is mainly used to find out SYSTEM
 790 * supported by the FW.
 791 */
 792int
 793megasas_sync_map_info(struct megasas_instance *instance)
 794{
 795        int ret = 0, i;
 796        struct megasas_cmd *cmd;
 797        struct megasas_dcmd_frame *dcmd;
 798        u32 size_sync_info, num_lds;
 799        struct fusion_context *fusion;
 800        struct MR_LD_TARGET_SYNC *ci = NULL;
 801        struct MR_FW_RAID_MAP_ALL *map;
 802        struct MR_LD_RAID  *raid;
 803        struct MR_LD_TARGET_SYNC *ld_sync;
 804        dma_addr_t ci_h = 0;
 805        u32 size_map_info;
 806
 807        cmd = megasas_get_cmd(instance);
 808
 809        if (!cmd) {
 810                printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
 811                       "info.\n");
 812                return -ENOMEM;
 813        }
 814
 815        fusion = instance->ctrl_context;
 816
 817        if (!fusion) {
 818                megasas_return_cmd(instance, cmd);
 819                return 1;
 820        }
 821
 822        map = fusion->ld_map[instance->map_id & 1];
 823
 824        num_lds = le32_to_cpu(map->raidMap.ldCount);
 825
 826        dcmd = &cmd->frame->dcmd;
 827
 828        size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
 829
 830        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
 831
 832        ci = (struct MR_LD_TARGET_SYNC *)
 833          fusion->ld_map[(instance->map_id - 1) & 1];
 834        memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
 835
 836        ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
 837
 838        ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
 839
 840        for (i = 0; i < num_lds; i++, ld_sync++) {
 841                raid = MR_LdRaidGet(i, map);
 842                ld_sync->targetId = MR_GetLDTgtId(i, map);
 843                ld_sync->seqNum = raid->seqNum;
 844        }
 845
 846        size_map_info = sizeof(struct MR_FW_RAID_MAP) +
 847                (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
 848
 849        dcmd->cmd = MFI_CMD_DCMD;
 850        dcmd->cmd_status = 0xFF;
 851        dcmd->sge_count = 1;
 852        dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
 853        dcmd->timeout = 0;
 854        dcmd->pad_0 = 0;
 855        dcmd->data_xfer_len = cpu_to_le32(size_map_info);
 856        dcmd->mbox.b[0] = num_lds;
 857        dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
 858        dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
 859        dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
 860        dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
 861
 862        instance->map_update_cmd = cmd;
 863
 864        instance->instancet->issue_dcmd(instance, cmd);
 865
 866        return ret;
 867}
 868
 869/*
 870 * meagasas_display_intel_branding - Display branding string
 871 * @instance: per adapter object
 872 *
 873 * Return nothing.
 874 */
 875static void
 876megasas_display_intel_branding(struct megasas_instance *instance)
 877{
 878        if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
 879                return;
 880
 881        switch (instance->pdev->device) {
 882        case PCI_DEVICE_ID_LSI_INVADER:
 883                switch (instance->pdev->subsystem_device) {
 884                case MEGARAID_INTEL_RS3DC080_SSDID:
 885                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 886                                instance->host->host_no,
 887                                MEGARAID_INTEL_RS3DC080_BRANDING);
 888                        break;
 889                case MEGARAID_INTEL_RS3DC040_SSDID:
 890                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 891                                instance->host->host_no,
 892                                MEGARAID_INTEL_RS3DC040_BRANDING);
 893                        break;
 894                case MEGARAID_INTEL_RS3SC008_SSDID:
 895                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 896                                instance->host->host_no,
 897                                MEGARAID_INTEL_RS3SC008_BRANDING);
 898                        break;
 899                case MEGARAID_INTEL_RS3MC044_SSDID:
 900                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 901                                instance->host->host_no,
 902                                MEGARAID_INTEL_RS3MC044_BRANDING);
 903                        break;
 904                default:
 905                        break;
 906                }
 907                break;
 908        case PCI_DEVICE_ID_LSI_FURY:
 909                switch (instance->pdev->subsystem_device) {
 910                case MEGARAID_INTEL_RS3WC080_SSDID:
 911                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 912                                instance->host->host_no,
 913                                MEGARAID_INTEL_RS3WC080_BRANDING);
 914                        break;
 915                case MEGARAID_INTEL_RS3WC040_SSDID:
 916                        dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
 917                                instance->host->host_no,
 918                                MEGARAID_INTEL_RS3WC040_BRANDING);
 919                        break;
 920                default:
 921                        break;
 922                }
 923                break;
 924        default:
 925                break;
 926        }
 927}
 928
 929/**
 930 * megasas_init_adapter_fusion -        Initializes the FW
 931 * @instance:           Adapter soft state
 932 *
 933 * This is the main function for initializing firmware.
 934 */
 935u32
 936megasas_init_adapter_fusion(struct megasas_instance *instance)
 937{
 938        struct megasas_register_set __iomem *reg_set;
 939        struct fusion_context *fusion;
 940        u32 max_cmd;
 941        int i = 0, count;
 942
 943        fusion = instance->ctrl_context;
 944
 945        reg_set = instance->reg_set;
 946
 947        /*
 948         * Get various operational parameters from status register
 949         */
 950        instance->max_fw_cmds =
 951                instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
 952        instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
 953
 954        /*
 955         * Reduce the max supported cmds by 1. This is to ensure that the
 956         * reply_q_sz (1 more than the max cmd that driver may send)
 957         * does not exceed max cmds that the FW can support
 958         */
 959        instance->max_fw_cmds = instance->max_fw_cmds-1;
 960        /* Only internal cmds (DCMD) need to have MFI frames */
 961        instance->max_mfi_cmds = MEGASAS_INT_CMDS;
 962
 963        max_cmd = instance->max_fw_cmds;
 964
 965        fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
 966
 967        fusion->request_alloc_sz =
 968                sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
 969        fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
 970                *(fusion->reply_q_depth);
 971        fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
 972                (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
 973                 (max_cmd + 1)); /* Extra 1 for SMID 0 */
 974
 975        fusion->max_sge_in_main_msg =
 976          (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
 977           offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
 978
 979        fusion->max_sge_in_chain =
 980                MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
 981
 982        instance->max_num_sge = fusion->max_sge_in_main_msg +
 983                fusion->max_sge_in_chain - 2;
 984
 985        /* Used for pass thru MFI frame (DCMD) */
 986        fusion->chain_offset_mfi_pthru =
 987                offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
 988
 989        fusion->chain_offset_io_request =
 990                (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
 991                 sizeof(union MPI2_SGE_IO_UNION))/16;
 992
 993        count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
 994        for (i = 0 ; i < count; i++)
 995                fusion->last_reply_idx[i] = 0;
 996
 997        /*
 998         * Allocate memory for descriptors
 999         * Create a pool of commands
1000         */
1001        if (megasas_alloc_cmds(instance))
1002                goto fail_alloc_mfi_cmds;
1003        if (megasas_alloc_cmds_fusion(instance))
1004                goto fail_alloc_cmds;
1005
1006        if (megasas_ioc_init_fusion(instance))
1007                goto fail_ioc_init;
1008
1009        megasas_display_intel_branding(instance);
1010
1011        instance->flag_ieee = 1;
1012
1013        fusion->map_sz =  sizeof(struct MR_FW_RAID_MAP) +
1014          (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
1015
1016        fusion->fast_path_io = 0;
1017
1018        for (i = 0; i < 2; i++) {
1019                fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1020                                                       fusion->map_sz,
1021                                                       &fusion->ld_map_phys[i],
1022                                                       GFP_KERNEL);
1023                if (!fusion->ld_map[i]) {
1024                        printk(KERN_ERR "megasas: Could not allocate memory "
1025                               "for map info\n");
1026                        goto fail_map_info;
1027                }
1028        }
1029
1030        if (!megasas_get_map_info(instance))
1031                megasas_sync_map_info(instance);
1032
1033        return 0;
1034
1035fail_map_info:
1036        if (i == 1)
1037                dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
1038                                  fusion->ld_map[0], fusion->ld_map_phys[0]);
1039fail_ioc_init:
1040        megasas_free_cmds_fusion(instance);
1041fail_alloc_cmds:
1042        megasas_free_cmds(instance);
1043fail_alloc_mfi_cmds:
1044        return 1;
1045}
1046
1047/**
1048 * megasas_fire_cmd_fusion -    Sends command to the FW
1049 * @frame_phys_addr :           Physical address of cmd
1050 * @frame_count :               Number of frames for the command
1051 * @regs :                      MFI register set
1052 */
1053void
1054megasas_fire_cmd_fusion(struct megasas_instance *instance,
1055                        dma_addr_t req_desc_lo,
1056                        u32 req_desc_hi,
1057                        struct megasas_register_set __iomem *regs)
1058{
1059        unsigned long flags;
1060
1061        spin_lock_irqsave(&instance->hba_lock, flags);
1062
1063        writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1064        writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
1065        spin_unlock_irqrestore(&instance->hba_lock, flags);
1066}
1067
1068/**
1069 * map_cmd_status -     Maps FW cmd status to OS cmd status
1070 * @cmd :               Pointer to cmd
1071 * @status :            status of cmd returned by FW
1072 * @ext_status :        ext status of cmd returned by FW
1073 */
1074
1075void
1076map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1077{
1078
1079        switch (status) {
1080
1081        case MFI_STAT_OK:
1082                cmd->scmd->result = DID_OK << 16;
1083                break;
1084
1085        case MFI_STAT_SCSI_IO_FAILED:
1086        case MFI_STAT_LD_INIT_IN_PROGRESS:
1087                cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1088                break;
1089
1090        case MFI_STAT_SCSI_DONE_WITH_ERROR:
1091
1092                cmd->scmd->result = (DID_OK << 16) | ext_status;
1093                if (ext_status == SAM_STAT_CHECK_CONDITION) {
1094                        memset(cmd->scmd->sense_buffer, 0,
1095                               SCSI_SENSE_BUFFERSIZE);
1096                        memcpy(cmd->scmd->sense_buffer, cmd->sense,
1097                               SCSI_SENSE_BUFFERSIZE);
1098                        cmd->scmd->result |= DRIVER_SENSE << 24;
1099                }
1100                break;
1101
1102        case MFI_STAT_LD_OFFLINE:
1103        case MFI_STAT_DEVICE_NOT_FOUND:
1104                cmd->scmd->result = DID_BAD_TARGET << 16;
1105                break;
1106        case MFI_STAT_CONFIG_SEQ_MISMATCH:
1107                cmd->scmd->result = DID_IMM_RETRY << 16;
1108                break;
1109        default:
1110                printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1111                cmd->scmd->result = DID_ERROR << 16;
1112                break;
1113        }
1114}
1115
1116/**
1117 * megasas_make_sgl_fusion -    Prepares 32-bit SGL
1118 * @instance:           Adapter soft state
1119 * @scp:                SCSI command from the mid-layer
1120 * @sgl_ptr:            SGL to be filled in
1121 * @cmd:                cmd we are working on
1122 *
1123 * If successful, this function returns the number of SG elements.
1124 */
1125static int
1126megasas_make_sgl_fusion(struct megasas_instance *instance,
1127                        struct scsi_cmnd *scp,
1128                        struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1129                        struct megasas_cmd_fusion *cmd)
1130{
1131        int i, sg_processed, sge_count;
1132        struct scatterlist *os_sgl;
1133        struct fusion_context *fusion;
1134
1135        fusion = instance->ctrl_context;
1136
1137        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1138                (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1139                struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1140                sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1141                sgl_ptr_end->Flags = 0;
1142        }
1143
1144        sge_count = scsi_dma_map(scp);
1145
1146        BUG_ON(sge_count < 0);
1147
1148        if (sge_count > instance->max_num_sge || !sge_count)
1149                return sge_count;
1150
1151        scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1152                sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1153                sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1154                sgl_ptr->Flags = 0;
1155                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1156                        (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1157                        if (i == sge_count - 1)
1158                                sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1159                }
1160                sgl_ptr++;
1161
1162                sg_processed = i + 1;
1163
1164                if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
1165                    (sge_count > fusion->max_sge_in_main_msg)) {
1166
1167                        struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1168                        if ((instance->pdev->device ==
1169                                PCI_DEVICE_ID_LSI_INVADER) ||
1170                                (instance->pdev->device ==
1171                                PCI_DEVICE_ID_LSI_FURY)) {
1172                                if ((le16_to_cpu(cmd->io_request->IoFlags) &
1173                                        MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1174                                        MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1175                                        cmd->io_request->ChainOffset =
1176                                                fusion->
1177                                                chain_offset_io_request;
1178                                else
1179                                        cmd->io_request->ChainOffset = 0;
1180                        } else
1181                                cmd->io_request->ChainOffset =
1182                                        fusion->chain_offset_io_request;
1183
1184                        sg_chain = sgl_ptr;
1185                        /* Prepare chain element */
1186                        sg_chain->NextChainOffset = 0;
1187                        if ((instance->pdev->device ==
1188                                PCI_DEVICE_ID_LSI_INVADER) ||
1189                                (instance->pdev->device ==
1190                                PCI_DEVICE_ID_LSI_FURY))
1191                                sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1192                        else
1193                                sg_chain->Flags =
1194                                        (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1195                                         MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1196                        sg_chain->Length =  cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1197                        sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1198
1199                        sgl_ptr =
1200                          (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1201                }
1202        }
1203
1204        return sge_count;
1205}
1206
1207/**
1208 * megasas_set_pd_lba - Sets PD LBA
1209 * @cdb:                CDB
1210 * @cdb_len:            cdb length
1211 * @start_blk:          Start block of IO
1212 *
1213 * Used to set the PD LBA in CDB for FP IOs
1214 */
1215void
1216megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1217                   struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1218                   struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1219{
1220        struct MR_LD_RAID *raid;
1221        u32 ld;
1222        u64 start_blk = io_info->pdBlock;
1223        u8 *cdb = io_request->CDB.CDB32;
1224        u32 num_blocks = io_info->numBlocks;
1225        u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1226
1227        /* Check if T10 PI (DIF) is enabled for this LD */
1228        ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1229        raid = MR_LdRaidGet(ld, local_map_ptr);
1230        if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1231                memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1232                cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1233                cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;
1234
1235                if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1236                        cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1237                else
1238                        cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1239                cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1240
1241                /* LBA */
1242                cdb[12] = (u8)((start_blk >> 56) & 0xff);
1243                cdb[13] = (u8)((start_blk >> 48) & 0xff);
1244                cdb[14] = (u8)((start_blk >> 40) & 0xff);
1245                cdb[15] = (u8)((start_blk >> 32) & 0xff);
1246                cdb[16] = (u8)((start_blk >> 24) & 0xff);
1247                cdb[17] = (u8)((start_blk >> 16) & 0xff);
1248                cdb[18] = (u8)((start_blk >> 8) & 0xff);
1249                cdb[19] = (u8)(start_blk & 0xff);
1250
1251                /* Logical block reference tag */
1252                io_request->CDB.EEDP32.PrimaryReferenceTag =
1253                        cpu_to_be32(ref_tag);
1254                io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1255                io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1256
1257                /* Transfer length */
1258                cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1259                cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1260                cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1261                cdb[31] = (u8)(num_blocks & 0xff);
1262
1263                /* set SCSI IO EEDPFlags */
1264                if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1265                        io_request->EEDPFlags = cpu_to_le16(
1266                                MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
1267                                MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1268                                MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1269                                MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1270                                MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1271                } else {
1272                        io_request->EEDPFlags = cpu_to_le16(
1273                                MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1274                                MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1275                }
1276                io_request->Control |= cpu_to_le32((0x4 << 26));
1277                io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1278        } else {
1279                /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1280                if (((cdb_len == 12) || (cdb_len == 16)) &&
1281                    (start_blk <= 0xffffffff)) {
1282                        if (cdb_len == 16) {
1283                                opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1284                                flagvals = cdb[1];
1285                                groupnum = cdb[14];
1286                                control = cdb[15];
1287                        } else {
1288                                opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1289                                flagvals = cdb[1];
1290                                groupnum = cdb[10];
1291                                control = cdb[11];
1292                        }
1293
1294                        memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1295
1296                        cdb[0] = opcode;
1297                        cdb[1] = flagvals;
1298                        cdb[6] = groupnum;
1299                        cdb[9] = control;
1300
1301                        /* Transfer length */
1302                        cdb[8] = (u8)(num_blocks & 0xff);
1303                        cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1304
1305                        io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1306                        cdb_len = 10;
1307                } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1308                        /* Convert to 16 byte CDB for large LBA's */
1309                        switch (cdb_len) {
1310                        case 6:
1311                                opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1312                                control = cdb[5];
1313                                break;
1314                        case 10:
1315                                opcode =
1316                                        cdb[0] == READ_10 ? READ_16 : WRITE_16;
1317                                flagvals = cdb[1];
1318                                groupnum = cdb[6];
1319                                control = cdb[9];
1320                                break;
1321                        case 12:
1322                                opcode =
1323                                        cdb[0] == READ_12 ? READ_16 : WRITE_16;
1324                                flagvals = cdb[1];
1325                                groupnum = cdb[10];
1326                                control = cdb[11];
1327                                break;
1328                        }
1329
1330                        memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1331
1332                        cdb[0] = opcode;
1333                        cdb[1] = flagvals;
1334                        cdb[14] = groupnum;
1335                        cdb[15] = control;
1336
1337                        /* Transfer length */
1338                        cdb[13] = (u8)(num_blocks & 0xff);
1339                        cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1340                        cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1341                        cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1342
1343                        io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1344                        cdb_len = 16;
1345                }
1346
1347                /* Normal case, just load LBA here */
1348                switch (cdb_len) {
1349                case 6:
1350                {
1351                        u8 val = cdb[1] & 0xE0;
1352                        cdb[3] = (u8)(start_blk & 0xff);
1353                        cdb[2] = (u8)((start_blk >> 8) & 0xff);
1354                        cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1355                        break;
1356                }
1357                case 10:
1358                        cdb[5] = (u8)(start_blk & 0xff);
1359                        cdb[4] = (u8)((start_blk >> 8) & 0xff);
1360                        cdb[3] = (u8)((start_blk >> 16) & 0xff);
1361                        cdb[2] = (u8)((start_blk >> 24) & 0xff);
1362                        break;
1363                case 12:
1364                        cdb[5]    = (u8)(start_blk & 0xff);
1365                        cdb[4]    = (u8)((start_blk >> 8) & 0xff);
1366                        cdb[3]    = (u8)((start_blk >> 16) & 0xff);
1367                        cdb[2]    = (u8)((start_blk >> 24) & 0xff);
1368                        break;
1369                case 16:
1370                        cdb[9]    = (u8)(start_blk & 0xff);
1371                        cdb[8]    = (u8)((start_blk >> 8) & 0xff);
1372                        cdb[7]    = (u8)((start_blk >> 16) & 0xff);
1373                        cdb[6]    = (u8)((start_blk >> 24) & 0xff);
1374                        cdb[5]    = (u8)((start_blk >> 32) & 0xff);
1375                        cdb[4]    = (u8)((start_blk >> 40) & 0xff);
1376                        cdb[3]    = (u8)((start_blk >> 48) & 0xff);
1377                        cdb[2]    = (u8)((start_blk >> 56) & 0xff);
1378                        break;
1379                }
1380        }
1381}
1382
1383/**
1384 * megasas_build_ldio_fusion -  Prepares IOs to devices
1385 * @instance:           Adapter soft state
1386 * @scp:                SCSI command
1387 * @cmd:                Command to be prepared
1388 *
1389 * Prepares the io_request and chain elements (sg_frame) for IO
1390 * The IO can be for PD (Fast Path) or LD
1391 */
1392void
1393megasas_build_ldio_fusion(struct megasas_instance *instance,
1394                          struct scsi_cmnd *scp,
1395                          struct megasas_cmd_fusion *cmd)
1396{
1397        u8 fp_possible;
1398        u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1399        struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1400        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1401        struct IO_REQUEST_INFO io_info;
1402        struct fusion_context *fusion;
1403        struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1404        u8 *raidLUN;
1405
1406        device_id = MEGASAS_DEV_INDEX(instance, scp);
1407
1408        fusion = instance->ctrl_context;
1409
1410        io_request = cmd->io_request;
1411        io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1412        io_request->RaidContext.status = 0;
1413        io_request->RaidContext.exStatus = 0;
1414
1415        req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1416
1417        start_lba_lo = 0;
1418        start_lba_hi = 0;
1419        fp_possible = 0;
1420
1421        /*
1422         * 6-byte READ(0x08) or WRITE(0x0A) cdb
1423         */
1424        if (scp->cmd_len == 6) {
1425                datalength = (u32) scp->cmnd[4];
1426                start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1427                        ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1428
1429                start_lba_lo &= 0x1FFFFF;
1430        }
1431
1432        /*
1433         * 10-byte READ(0x28) or WRITE(0x2A) cdb
1434         */
1435        else if (scp->cmd_len == 10) {
1436                datalength = (u32) scp->cmnd[8] |
1437                        ((u32) scp->cmnd[7] << 8);
1438                start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1439                        ((u32) scp->cmnd[3] << 16) |
1440                        ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1441        }
1442
1443        /*
1444         * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1445         */
1446        else if (scp->cmd_len == 12) {
1447                datalength = ((u32) scp->cmnd[6] << 24) |
1448                        ((u32) scp->cmnd[7] << 16) |
1449                        ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1450                start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1451                        ((u32) scp->cmnd[3] << 16) |
1452                        ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1453        }
1454
1455        /*
1456         * 16-byte READ(0x88) or WRITE(0x8A) cdb
1457         */
1458        else if (scp->cmd_len == 16) {
1459                datalength = ((u32) scp->cmnd[10] << 24) |
1460                        ((u32) scp->cmnd[11] << 16) |
1461                        ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1462                start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1463                        ((u32) scp->cmnd[7] << 16) |
1464                        ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1465
1466                start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1467                        ((u32) scp->cmnd[3] << 16) |
1468                        ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1469        }
1470
1471        memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1472        io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1473        io_info.numBlocks = datalength;
1474        io_info.ldTgtId = device_id;
1475        io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1476
1477        if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1478                io_info.isRead = 1;
1479
1480        local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1481
1482        if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1483             MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1484                io_request->RaidContext.regLockFlags  = 0;
1485                fp_possible = 0;
1486        } else {
1487                if (MR_BuildRaidContext(instance, &io_info,
1488                                        &io_request->RaidContext,
1489                                        local_map_ptr, &raidLUN))
1490                        fp_possible = io_info.fpOkForIo;
1491        }
1492
1493        /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1494           id by default, not CPU group id, otherwise all MSI-X queues won't
1495           be utilized */
1496        cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1497                smp_processor_id() % instance->msix_vectors : 0;
1498
1499        if (fp_possible) {
1500                megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1501                                   local_map_ptr, start_lba_lo);
1502                io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1503                cmd->request_desc->SCSIIO.RequestFlags =
1504                        (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1505                         << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1506                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1507                        (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1508                        if (io_request->RaidContext.regLockFlags ==
1509                            REGION_TYPE_UNUSED)
1510                                cmd->request_desc->SCSIIO.RequestFlags =
1511                                        (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1512                                        MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1513                        io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1514                        io_request->RaidContext.nseg = 0x1;
1515                        io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1516                        io_request->RaidContext.regLockFlags |=
1517                          (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1518                           MR_RL_FLAGS_SEQ_NUM_ENABLE);
1519                }
1520                if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1521                    (io_info.isRead)) {
1522                        io_info.devHandle =
1523                                get_updated_dev_handle(
1524                                        &fusion->load_balance_info[device_id],
1525                                        &io_info);
1526                        scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1527                } else
1528                        scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1529                cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1530                io_request->DevHandle = io_info.devHandle;
1531                /* populate the LUN field */
1532                memcpy(io_request->LUN, raidLUN, 8);
1533        } else {
1534                io_request->RaidContext.timeoutValue =
1535                        cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
1536                cmd->request_desc->SCSIIO.RequestFlags =
1537                        (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1538                         << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1539                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1540                        (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1541                        if (io_request->RaidContext.regLockFlags ==
1542                            REGION_TYPE_UNUSED)
1543                                cmd->request_desc->SCSIIO.RequestFlags =
1544                                        (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1545                                        MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1546                        io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1547                        io_request->RaidContext.regLockFlags |=
1548                                (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1549                                 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1550                        io_request->RaidContext.nseg = 0x1;
1551                }
1552                io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1553                io_request->DevHandle = cpu_to_le16(device_id);
1554        } /* Not FP */
1555}
1556
1557/**
1558 * megasas_build_dcdb_fusion -  Prepares IOs to devices
1559 * @instance:           Adapter soft state
1560 * @scp:                SCSI command
1561 * @cmd:                Command to be prepared
1562 *
1563 * Prepares the io_request frame for non-io cmds
1564 */
1565static void
1566megasas_build_dcdb_fusion(struct megasas_instance *instance,
1567                          struct scsi_cmnd *scmd,
1568                          struct megasas_cmd_fusion *cmd)
1569{
1570        u32 device_id;
1571        struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1572        u16 pd_index = 0;
1573        struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1574        struct fusion_context *fusion = instance->ctrl_context;
1575        u8                          span, physArm;
1576        u16                         devHandle;
1577        u32                         ld, arRef, pd;
1578        struct MR_LD_RAID                  *raid;
1579        struct RAID_CONTEXT                *pRAID_Context;
1580
1581        io_request = cmd->io_request;
1582        device_id = MEGASAS_DEV_INDEX(instance, scmd);
1583        pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1584                +scmd->device->id;
1585        local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1586
1587        io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1588
1589
1590        /* Check if this is a system PD I/O */
1591        if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1592            instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1593                io_request->Function = 0;
1594                if (fusion->fast_path_io)
1595                        io_request->DevHandle =
1596                        local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1597                io_request->RaidContext.timeoutValue =
1598                        local_map_ptr->raidMap.fpPdIoTimeoutSec;
1599                io_request->RaidContext.regLockFlags = 0;
1600                io_request->RaidContext.regLockRowLBA = 0;
1601                io_request->RaidContext.regLockLength = 0;
1602                io_request->RaidContext.RAIDFlags =
1603                        MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1604                        MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1605                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1606                        (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1607                        io_request->IoFlags |=
1608                                MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1609                cmd->request_desc->SCSIIO.RequestFlags =
1610                        (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1611                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1612                cmd->request_desc->SCSIIO.DevHandle =
1613                        local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1614                /*
1615                 * If the command is for the tape device, set the
1616                 * FP timeout to the os layer timeout value.
1617                 */
1618                if (scmd->device->type == TYPE_TAPE) {
1619                        if ((scmd->request->timeout / HZ) > 0xFFFF)
1620                                io_request->RaidContext.timeoutValue =
1621                                        0xFFFF;
1622                        else
1623                                io_request->RaidContext.timeoutValue =
1624                                        scmd->request->timeout / HZ;
1625                }
1626        } else {
1627                if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1628                        goto NonFastPath;
1629
1630                ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1631                if ((ld >= MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io))
1632                        goto NonFastPath;
1633
1634                raid = MR_LdRaidGet(ld, local_map_ptr);
1635
1636                /* check if this LD is FP capable */
1637                if (!(raid->capability.fpNonRWCapable))
1638                        /* not FP capable, send as non-FP */
1639                        goto NonFastPath;
1640
1641                /* get RAID_Context pointer */
1642                pRAID_Context = &io_request->RaidContext;
1643
1644                /* set RAID context values */
1645                pRAID_Context->regLockFlags     = REGION_TYPE_SHARED_READ;
1646                pRAID_Context->timeoutValue     = raid->fpIoTimeoutForLd;
1647                pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1648                pRAID_Context->regLockRowLBA    = 0;
1649                pRAID_Context->regLockLength    = 0;
1650                pRAID_Context->configSeqNum     = raid->seqNum;
1651
1652                /* get the DevHandle for the PD (since this is
1653                   fpNonRWCapable, this is a single disk RAID0) */
1654                span = physArm = 0;
1655                arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1656                pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1657                devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1658
1659                /* build request descriptor */
1660                cmd->request_desc->SCSIIO.RequestFlags =
1661                        (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1662                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1663                cmd->request_desc->SCSIIO.DevHandle = devHandle;
1664
1665                /* populate the LUN field */
1666                memcpy(io_request->LUN, raid->LUN, 8);
1667
1668                /* build the raidScsiIO structure */
1669                io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1670                io_request->DevHandle = devHandle;
1671
1672                return;
1673
1674NonFastPath:
1675                io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1676                io_request->DevHandle = cpu_to_le16(device_id);
1677                cmd->request_desc->SCSIIO.RequestFlags =
1678                        (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1679                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1680        }
1681        io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1682        io_request->LUN[1] = scmd->device->lun;
1683}
1684
1685/**
1686 * megasas_build_io_fusion -    Prepares IOs to devices
1687 * @instance:           Adapter soft state
1688 * @scp:                SCSI command
1689 * @cmd:                Command to be prepared
1690 *
1691 * Invokes helper functions to prepare request frames
1692 * and sets flags appropriate for IO/Non-IO cmd
1693 */
1694int
1695megasas_build_io_fusion(struct megasas_instance *instance,
1696                        struct scsi_cmnd *scp,
1697                        struct megasas_cmd_fusion *cmd)
1698{
1699        u32 device_id, sge_count;
1700        struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1701
1702        device_id = MEGASAS_DEV_INDEX(instance, scp);
1703
1704        /* Zero out some fields so they don't get reused */
1705        io_request->LUN[1] = 0;
1706        io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1707        io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1708        io_request->EEDPFlags = 0;
1709        io_request->Control = 0;
1710        io_request->EEDPBlockSize = 0;
1711        io_request->ChainOffset = 0;
1712        io_request->RaidContext.RAIDFlags = 0;
1713        io_request->RaidContext.Type = 0;
1714        io_request->RaidContext.nseg = 0;
1715
1716        memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1717        /*
1718         * Just the CDB length,rest of the Flags are zero
1719         * This will be modified for FP in build_ldio_fusion
1720         */
1721        io_request->IoFlags = cpu_to_le16(scp->cmd_len);
1722
1723        if (megasas_is_ldio(scp))
1724                megasas_build_ldio_fusion(instance, scp, cmd);
1725        else
1726                megasas_build_dcdb_fusion(instance, scp, cmd);
1727
1728        /*
1729         * Construct SGL
1730         */
1731
1732        sge_count =
1733                megasas_make_sgl_fusion(instance, scp,
1734                                        (struct MPI25_IEEE_SGE_CHAIN64 *)
1735                                        &io_request->SGL, cmd);
1736
1737        if (sge_count > instance->max_num_sge) {
1738                printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1739                       "max (0x%x) allowed\n", sge_count,
1740                       instance->max_num_sge);
1741                return 1;
1742        }
1743
1744        io_request->RaidContext.numSGE = sge_count;
1745
1746        io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
1747
1748        if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1749                io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
1750        else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1751                io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
1752
1753        io_request->SGLOffset0 =
1754                offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1755
1756        io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
1757        io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1758
1759        cmd->scmd = scp;
1760        scp->SCp.ptr = (char *)cmd;
1761
1762        return 0;
1763}
1764
1765union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1766megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1767{
1768        u8 *p;
1769        struct fusion_context *fusion;
1770
1771        if (index >= instance->max_fw_cmds) {
1772                printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1773                       "descriptor\n", index);
1774                return NULL;
1775        }
1776        fusion = instance->ctrl_context;
1777        p = fusion->req_frames_desc
1778                +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1779
1780        return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1781}
1782
1783/**
1784 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1785 *                                     issuing non IOCTL cmd
1786 * @instance:                   Adapter soft state
1787 * @scmd:                       pointer to scsi cmd from OS
1788 */
1789static u32
1790megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1791                                   struct scsi_cmnd *scmd)
1792{
1793        struct megasas_cmd_fusion *cmd;
1794        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1795        u32 index;
1796        struct fusion_context *fusion;
1797
1798        fusion = instance->ctrl_context;
1799
1800        cmd = megasas_get_cmd_fusion(instance);
1801        if (!cmd)
1802                return SCSI_MLQUEUE_HOST_BUSY;
1803
1804        index = cmd->index;
1805
1806        req_desc = megasas_get_request_descriptor(instance, index-1);
1807        if (!req_desc)
1808                return 1;
1809
1810        req_desc->Words = 0;
1811        cmd->request_desc = req_desc;
1812
1813        if (megasas_build_io_fusion(instance, scmd, cmd)) {
1814                megasas_return_cmd_fusion(instance, cmd);
1815                printk(KERN_ERR "megasas: Error building command.\n");
1816                cmd->request_desc = NULL;
1817                return 1;
1818        }
1819
1820        req_desc = cmd->request_desc;
1821        req_desc->SCSIIO.SMID = cpu_to_le16(index);
1822
1823        if (cmd->io_request->ChainOffset != 0 &&
1824            cmd->io_request->ChainOffset != 0xF)
1825                printk(KERN_ERR "megasas: The chain offset value is not "
1826                       "correct : %x\n", cmd->io_request->ChainOffset);
1827
1828        /*
1829         * Issue the command to the FW
1830         */
1831        atomic_inc(&instance->fw_outstanding);
1832
1833        instance->instancet->fire_cmd(instance,
1834                                      req_desc->u.low, req_desc->u.high,
1835                                      instance->reg_set);
1836
1837        return 0;
1838}
1839
1840/**
1841 * complete_cmd_fusion -        Completes command
1842 * @instance:                   Adapter soft state
1843 * Completes all commands that is in reply descriptor queue
1844 */
1845int
1846complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1847{
1848        union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1849        struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1850        struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1851        struct fusion_context *fusion;
1852        struct megasas_cmd *cmd_mfi;
1853        struct megasas_cmd_fusion *cmd_fusion;
1854        u16 smid, num_completed;
1855        u8 reply_descript_type, arm;
1856        u32 status, extStatus, device_id;
1857        union desc_value d_val;
1858        struct LD_LOAD_BALANCE_INFO *lbinfo;
1859
1860        fusion = instance->ctrl_context;
1861
1862        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1863                return IRQ_HANDLED;
1864
1865        desc = fusion->reply_frames_desc;
1866        desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1867                 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1868                fusion->last_reply_idx[MSIxIndex];
1869
1870        reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1871
1872        d_val.word = desc->Words;
1873
1874        reply_descript_type = reply_desc->ReplyFlags &
1875                MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1876
1877        if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1878                return IRQ_NONE;
1879
1880        num_completed = 0;
1881
1882        while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1883                smid = le16_to_cpu(reply_desc->SMID);
1884
1885                cmd_fusion = fusion->cmd_list[smid - 1];
1886
1887                scsi_io_req =
1888                        (struct MPI2_RAID_SCSI_IO_REQUEST *)
1889                  cmd_fusion->io_request;
1890
1891                if (cmd_fusion->scmd)
1892                        cmd_fusion->scmd->SCp.ptr = NULL;
1893
1894                status = scsi_io_req->RaidContext.status;
1895                extStatus = scsi_io_req->RaidContext.exStatus;
1896
1897                switch (scsi_io_req->Function) {
1898                case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
1899                        /* Update load balancing info */
1900                        device_id = MEGASAS_DEV_INDEX(instance,
1901                                                      cmd_fusion->scmd);
1902                        lbinfo = &fusion->load_balance_info[device_id];
1903                        if (cmd_fusion->scmd->SCp.Status &
1904                            MEGASAS_LOAD_BALANCE_FLAG) {
1905                                arm = lbinfo->raid1DevHandle[0] ==
1906                                        cmd_fusion->io_request->DevHandle ? 0 :
1907                                        1;
1908                                atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1909                                cmd_fusion->scmd->SCp.Status &=
1910                                        ~MEGASAS_LOAD_BALANCE_FLAG;
1911                        }
1912                        if (reply_descript_type ==
1913                            MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1914                                if (megasas_dbg_lvl == 5)
1915                                        printk(KERN_ERR "\nmegasas: FAST Path "
1916                                               "IO Success\n");
1917                        }
1918                        /* Fall thru and complete IO */
1919                case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1920                        /* Map the FW Cmd Status */
1921                        map_cmd_status(cmd_fusion, status, extStatus);
1922                        scsi_dma_unmap(cmd_fusion->scmd);
1923                        cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1924                        scsi_io_req->RaidContext.status = 0;
1925                        scsi_io_req->RaidContext.exStatus = 0;
1926                        megasas_return_cmd_fusion(instance, cmd_fusion);
1927                        atomic_dec(&instance->fw_outstanding);
1928
1929                        break;
1930                case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1931                        cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1932                        megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1933                        cmd_fusion->flags = 0;
1934                        megasas_return_cmd_fusion(instance, cmd_fusion);
1935
1936                        break;
1937                }
1938
1939                fusion->last_reply_idx[MSIxIndex]++;
1940                if (fusion->last_reply_idx[MSIxIndex] >=
1941                    fusion->reply_q_depth)
1942                        fusion->last_reply_idx[MSIxIndex] = 0;
1943
1944                desc->Words = ULLONG_MAX;
1945                num_completed++;
1946
1947                /* Get the next reply descriptor */
1948                if (!fusion->last_reply_idx[MSIxIndex])
1949                        desc = fusion->reply_frames_desc +
1950                                ((MSIxIndex * fusion->reply_alloc_sz)/
1951                                 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1952                else
1953                        desc++;
1954
1955                reply_desc =
1956                  (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1957
1958                d_val.word = desc->Words;
1959
1960                reply_descript_type = reply_desc->ReplyFlags &
1961                        MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1962
1963                if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1964                        break;
1965        }
1966
1967        if (!num_completed)
1968                return IRQ_NONE;
1969
1970        wmb();
1971        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1972                (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1973                writel(((MSIxIndex & 0x7) << 24) |
1974                        fusion->last_reply_idx[MSIxIndex],
1975                        instance->reply_post_host_index_addr[MSIxIndex/8]);
1976        else
1977                writel((MSIxIndex << 24) |
1978                        fusion->last_reply_idx[MSIxIndex],
1979                        instance->reply_post_host_index_addr[0]);
1980        megasas_check_and_restore_queue_depth(instance);
1981        return IRQ_HANDLED;
1982}
1983
1984/**
1985 * megasas_complete_cmd_dpc_fusion -    Completes command
1986 * @instance:                   Adapter soft state
1987 *
1988 * Tasklet to complete cmds
1989 */
1990void
1991megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1992{
1993        struct megasas_instance *instance =
1994                (struct megasas_instance *)instance_addr;
1995        unsigned long flags;
1996        u32 count, MSIxIndex;
1997
1998        count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1999
2000        /* If we have already declared adapter dead, donot complete cmds */
2001        spin_lock_irqsave(&instance->hba_lock, flags);
2002        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2003                spin_unlock_irqrestore(&instance->hba_lock, flags);
2004                return;
2005        }
2006        spin_unlock_irqrestore(&instance->hba_lock, flags);
2007
2008        for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2009                complete_cmd_fusion(instance, MSIxIndex);
2010}
2011
2012/**
2013 * megasas_isr_fusion - isr entry point
2014 */
2015irqreturn_t megasas_isr_fusion(int irq, void *devp)
2016{
2017        struct megasas_irq_context *irq_context = devp;
2018        struct megasas_instance *instance = irq_context->instance;
2019        u32 mfiStatus, fw_state;
2020
2021        if (instance->mask_interrupts)
2022                return IRQ_NONE;
2023
2024        if (!instance->msix_vectors) {
2025                mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2026                if (!mfiStatus)
2027                        return IRQ_NONE;
2028        }
2029
2030        /* If we are resetting, bail */
2031        if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2032                instance->instancet->clear_intr(instance->reg_set);
2033                return IRQ_HANDLED;
2034        }
2035
2036        if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2037                instance->instancet->clear_intr(instance->reg_set);
2038                /* If we didn't complete any commands, check for FW fault */
2039                fw_state = instance->instancet->read_fw_status_reg(
2040                        instance->reg_set) & MFI_STATE_MASK;
2041                if (fw_state == MFI_STATE_FAULT)
2042                        schedule_work(&instance->work_init);
2043        }
2044
2045        return IRQ_HANDLED;
2046}
2047
2048/**
2049 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2050 * @instance:                   Adapter soft state
2051 * mfi_cmd:                     megasas_cmd pointer
2052 *
2053 */
2054u8
2055build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2056                        struct megasas_cmd *mfi_cmd)
2057{
2058        struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2059        struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2060        struct megasas_cmd_fusion *cmd;
2061        struct fusion_context *fusion;
2062        struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2063
2064        cmd = megasas_get_cmd_fusion(instance);
2065        if (!cmd)
2066                return 1;
2067
2068        /*  Save the smid. To be used for returning the cmd */
2069        mfi_cmd->context.smid = cmd->index;
2070
2071        cmd->sync_cmd_idx = mfi_cmd->index;
2072
2073        /*
2074         * For cmds where the flag is set, store the flag and check
2075         * on completion. For cmds with this flag, don't call
2076         * megasas_complete_cmd
2077         */
2078
2079        if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2080                cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2081
2082        fusion = instance->ctrl_context;
2083        io_req = cmd->io_request;
2084
2085        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2086                (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2087                struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2088                        (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2089                sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2090                sgl_ptr_end->Flags = 0;
2091        }
2092
2093        mpi25_ieee_chain =
2094          (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2095
2096        io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2097        io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2098                                       SGL) / 4;
2099        io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2100
2101        mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
2102
2103        mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2104                MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2105
2106        mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
2107
2108        return 0;
2109}
2110
2111/**
2112 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2113 * @instance:                   Adapter soft state
2114 * @cmd:                        mfi cmd to build
2115 *
2116 */
2117union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2118build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2119{
2120        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2121        u16 index;
2122
2123        if (build_mpt_mfi_pass_thru(instance, cmd)) {
2124                printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2125                return NULL;
2126        }
2127
2128        index = cmd->context.smid;
2129
2130        req_desc = megasas_get_request_descriptor(instance, index - 1);
2131
2132        if (!req_desc)
2133                return NULL;
2134
2135        req_desc->Words = 0;
2136        req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2137                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2138
2139        req_desc->SCSIIO.SMID = cpu_to_le16(index);
2140
2141        return req_desc;
2142}
2143
2144/**
2145 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2146 * @instance:                   Adapter soft state
2147 * @cmd:                        mfi cmd pointer
2148 *
2149 */
2150void
2151megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2152                          struct megasas_cmd *cmd)
2153{
2154        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2155
2156        req_desc = build_mpt_cmd(instance, cmd);
2157        if (!req_desc) {
2158                printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2159                return;
2160        }
2161        instance->instancet->fire_cmd(instance, req_desc->u.low,
2162                                      req_desc->u.high, instance->reg_set);
2163}
2164
2165/**
2166 * megasas_release_fusion -     Reverses the FW initialization
2167 * @intance:                    Adapter soft state
2168 */
2169void
2170megasas_release_fusion(struct megasas_instance *instance)
2171{
2172        megasas_free_cmds(instance);
2173        megasas_free_cmds_fusion(instance);
2174
2175        iounmap(instance->reg_set);
2176
2177        pci_release_selected_regions(instance->pdev, instance->bar);
2178}
2179
2180/**
2181 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2182 * @regs:                       MFI register set
2183 */
2184static u32
2185megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2186{
2187        return readl(&(regs)->outbound_scratch_pad);
2188}
2189
2190/**
2191 * megasas_adp_reset_fusion -   For controller reset
2192 * @regs:                               MFI register set
2193 */
2194static int
2195megasas_adp_reset_fusion(struct megasas_instance *instance,
2196                         struct megasas_register_set __iomem *regs)
2197{
2198        return 0;
2199}
2200
2201/**
2202 * megasas_check_reset_fusion - For controller reset check
2203 * @regs:                               MFI register set
2204 */
2205static int
2206megasas_check_reset_fusion(struct megasas_instance *instance,
2207                           struct megasas_register_set __iomem *regs)
2208{
2209        return 0;
2210}
2211
2212/* This function waits for outstanding commands on fusion to complete */
2213int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2214{
2215        int i, outstanding, retval = 0;
2216        u32 fw_state;
2217
2218        for (i = 0; i < resetwaittime; i++) {
2219                /* Check if firmware is in fault state */
2220                fw_state = instance->instancet->read_fw_status_reg(
2221                        instance->reg_set) & MFI_STATE_MASK;
2222                if (fw_state == MFI_STATE_FAULT) {
2223                        printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2224                               " will reset adapter.\n");
2225                        retval = 1;
2226                        goto out;
2227                }
2228
2229                outstanding = atomic_read(&instance->fw_outstanding);
2230                if (!outstanding)
2231                        goto out;
2232
2233                if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2234                        printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2235                               "commands to complete\n", i, outstanding);
2236                        megasas_complete_cmd_dpc_fusion(
2237                                (unsigned long)instance);
2238                }
2239                msleep(1000);
2240        }
2241
2242        if (atomic_read(&instance->fw_outstanding)) {
2243                printk("megaraid_sas: pending commands remain after waiting, "
2244                       "will reset adapter.\n");
2245                retval = 1;
2246        }
2247out:
2248        return retval;
2249}
2250
2251void  megasas_reset_reply_desc(struct megasas_instance *instance)
2252{
2253        int i, count;
2254        struct fusion_context *fusion;
2255        union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2256
2257        fusion = instance->ctrl_context;
2258        count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2259        for (i = 0 ; i < count ; i++)
2260                fusion->last_reply_idx[i] = 0;
2261        reply_desc = fusion->reply_frames_desc;
2262        for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2263                reply_desc->Words = ULLONG_MAX;
2264}
2265
2266/* Core fusion reset function */
2267int megasas_reset_fusion(struct Scsi_Host *shost)
2268{
2269        int retval = SUCCESS, i, j, retry = 0;
2270        struct megasas_instance *instance;
2271        struct megasas_cmd_fusion *cmd_fusion;
2272        struct fusion_context *fusion;
2273        struct megasas_cmd *cmd_mfi;
2274        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2275        u32 host_diag, abs_state, status_reg, reset_adapter;
2276
2277        instance = (struct megasas_instance *)shost->hostdata;
2278        fusion = instance->ctrl_context;
2279
2280        if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2281                printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2282                       "returning FAILED.\n");
2283                return FAILED;
2284        }
2285
2286        mutex_lock(&instance->reset_mutex);
2287        set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2288        instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2289        instance->instancet->disable_intr(instance);
2290        msleep(1000);
2291
2292        /* First try waiting for commands to complete */
2293        if (megasas_wait_for_outstanding_fusion(instance)) {
2294                printk(KERN_WARNING "megaraid_sas: resetting fusion "
2295                       "adapter.\n");
2296                /* Now return commands back to the OS */
2297                for (i = 0 ; i < instance->max_fw_cmds; i++) {
2298                        cmd_fusion = fusion->cmd_list[i];
2299                        if (cmd_fusion->scmd) {
2300                                scsi_dma_unmap(cmd_fusion->scmd);
2301                                cmd_fusion->scmd->result = (DID_RESET << 16);
2302                                cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2303                                megasas_return_cmd_fusion(instance, cmd_fusion);
2304                                atomic_dec(&instance->fw_outstanding);
2305                        }
2306                }
2307
2308                status_reg = instance->instancet->read_fw_status_reg(
2309                        instance->reg_set);
2310                abs_state = status_reg & MFI_STATE_MASK;
2311                reset_adapter = status_reg & MFI_RESET_ADAPTER;
2312                if (instance->disableOnlineCtrlReset ||
2313                    (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2314                        /* Reset not supported, kill adapter */
2315                        printk(KERN_WARNING "megaraid_sas: Reset not supported"
2316                               ", killing adapter.\n");
2317                        megaraid_sas_kill_hba(instance);
2318                        instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2319                        retval = FAILED;
2320                        goto out;
2321                }
2322
2323                /* Now try to reset the chip */
2324                for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2325                        writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2326                               &instance->reg_set->fusion_seq_offset);
2327                        writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2328                               &instance->reg_set->fusion_seq_offset);
2329                        writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2330                               &instance->reg_set->fusion_seq_offset);
2331                        writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2332                               &instance->reg_set->fusion_seq_offset);
2333                        writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2334                               &instance->reg_set->fusion_seq_offset);
2335                        writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2336                               &instance->reg_set->fusion_seq_offset);
2337                        writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2338                               &instance->reg_set->fusion_seq_offset);
2339
2340                        /* Check that the diag write enable (DRWE) bit is on */
2341                        host_diag = readl(&instance->reg_set->fusion_host_diag);
2342                        retry = 0;
2343                        while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2344                                msleep(100);
2345                                host_diag =
2346                                readl(&instance->reg_set->fusion_host_diag);
2347                                if (retry++ == 100) {
2348                                        printk(KERN_WARNING "megaraid_sas: "
2349                                               "Host diag unlock failed!\n");
2350                                        break;
2351                                }
2352                        }
2353                        if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2354                                continue;
2355
2356                        /* Send chip reset command */
2357                        writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2358                               &instance->reg_set->fusion_host_diag);
2359                        msleep(3000);
2360
2361                        /* Make sure reset adapter bit is cleared */
2362                        host_diag = readl(&instance->reg_set->fusion_host_diag);
2363                        retry = 0;
2364                        while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2365                                msleep(100);
2366                                host_diag =
2367                                readl(&instance->reg_set->fusion_host_diag);
2368                                if (retry++ == 1000) {
2369                                        printk(KERN_WARNING "megaraid_sas: "
2370                                               "Diag reset adapter never "
2371                                               "cleared!\n");
2372                                        break;
2373                                }
2374                        }
2375                        if (host_diag & HOST_DIAG_RESET_ADAPTER)
2376                                continue;
2377
2378                        abs_state =
2379                                instance->instancet->read_fw_status_reg(
2380                                        instance->reg_set) & MFI_STATE_MASK;
2381                        retry = 0;
2382
2383                        while ((abs_state <= MFI_STATE_FW_INIT) &&
2384                               (retry++ < 1000)) {
2385                                msleep(100);
2386                                abs_state =
2387                                instance->instancet->read_fw_status_reg(
2388                                        instance->reg_set) & MFI_STATE_MASK;
2389                        }
2390                        if (abs_state <= MFI_STATE_FW_INIT) {
2391                                printk(KERN_WARNING "megaraid_sas: firmware "
2392                                       "state < MFI_STATE_FW_INIT, state = "
2393                                       "0x%x\n", abs_state);
2394                                continue;
2395                        }
2396
2397                        /* Wait for FW to become ready */
2398                        if (megasas_transition_to_ready(instance, 1)) {
2399                                printk(KERN_WARNING "megaraid_sas: Failed to "
2400                                       "transition controller to ready.\n");
2401                                continue;
2402                        }
2403
2404                        megasas_reset_reply_desc(instance);
2405                        if (megasas_ioc_init_fusion(instance)) {
2406                                printk(KERN_WARNING "megaraid_sas: "
2407                                       "megasas_ioc_init_fusion() failed!\n");
2408                                continue;
2409                        }
2410
2411                        clear_bit(MEGASAS_FUSION_IN_RESET,
2412                                  &instance->reset_flags);
2413                        instance->instancet->enable_intr(instance);
2414                        instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2415
2416                        /* Re-fire management commands */
2417                        for (j = 0 ; j < instance->max_fw_cmds; j++) {
2418                                cmd_fusion = fusion->cmd_list[j];
2419                                if (cmd_fusion->sync_cmd_idx !=
2420                                    (u32)ULONG_MAX) {
2421                                        cmd_mfi =
2422                                        instance->
2423                                        cmd_list[cmd_fusion->sync_cmd_idx];
2424                                        if (cmd_mfi->frame->dcmd.opcode ==
2425                                            MR_DCMD_LD_MAP_GET_INFO) {
2426                                                megasas_return_cmd(instance,
2427                                                                   cmd_mfi);
2428                                                megasas_return_cmd_fusion(
2429                                                        instance, cmd_fusion);
2430                                        } else  {
2431                                                req_desc =
2432                                                megasas_get_request_descriptor(
2433                                                        instance,
2434                                                        cmd_mfi->context.smid
2435                                                        -1);
2436                                                if (!req_desc)
2437                                                        printk(KERN_WARNING
2438                                                               "req_desc NULL"
2439                                                               "\n");
2440                                                else {
2441                                                        instance->instancet->
2442                                                        fire_cmd(instance,
2443                                                                 req_desc->
2444                                                                 u.low,
2445                                                                 req_desc->
2446                                                                 u.high,
2447                                                                 instance->
2448                                                                 reg_set);
2449                                                }
2450                                        }
2451                                }
2452                        }
2453
2454                        /* Reset load balance info */
2455                        memset(fusion->load_balance_info, 0,
2456                               sizeof(struct LD_LOAD_BALANCE_INFO)
2457                               *MAX_LOGICAL_DRIVES);
2458
2459                        if (!megasas_get_map_info(instance))
2460                                megasas_sync_map_info(instance);
2461
2462                        /* Adapter reset completed successfully */
2463                        printk(KERN_WARNING "megaraid_sas: Reset "
2464                               "successful.\n");
2465                        retval = SUCCESS;
2466                        goto out;
2467                }
2468                /* Reset failed, kill the adapter */
2469                printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2470                       "adapter.\n");
2471                megaraid_sas_kill_hba(instance);
2472                retval = FAILED;
2473        } else {
2474                clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2475                instance->instancet->enable_intr(instance);
2476                instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2477        }
2478out:
2479        clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2480        mutex_unlock(&instance->reset_mutex);
2481        return retval;
2482}
2483
2484/* Fusion OCR work queue */
2485void megasas_fusion_ocr_wq(struct work_struct *work)
2486{
2487        struct megasas_instance *instance =
2488                container_of(work, struct megasas_instance, work_init);
2489
2490        megasas_reset_fusion(instance->host);
2491}
2492
2493struct megasas_instance_template megasas_instance_template_fusion = {
2494        .fire_cmd = megasas_fire_cmd_fusion,
2495        .enable_intr = megasas_enable_intr_fusion,
2496        .disable_intr = megasas_disable_intr_fusion,
2497        .clear_intr = megasas_clear_intr_fusion,
2498        .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2499        .adp_reset = megasas_adp_reset_fusion,
2500        .check_reset = megasas_check_reset_fusion,
2501        .service_isr = megasas_isr_fusion,
2502        .tasklet = megasas_complete_cmd_dpc_fusion,
2503        .init_adapter = megasas_init_adapter_fusion,
2504        .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2505        .issue_dcmd = megasas_issue_dcmd_fusion,
2506};
2507