linux/drivers/scsi/megaraid.c
<<
>>
Prefs
   1/*
   2 *
   3 *                      Linux MegaRAID device driver
   4 *
   5 * Copyright (c) 2002  LSI Logic Corporation.
   6 *
   7 *         This program is free software; you can redistribute it and/or
   8 *         modify it under the terms of the GNU General Public License
   9 *         as published by the Free Software Foundation; either version
  10 *         2 of the License, or (at your option) any later version.
  11 *
  12 * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
  13 *        - fixes
  14 *        - speed-ups (list handling fixes, issued_list, optimizations.)
  15 *        - lots of cleanups.
  16 *
  17 * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
  18 *        - new-style, hotplug-aware pci probing and scsi registration
  19 *
  20 * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
  21 *                                              <Seokmann.Ju@lsil.com>
  22 *
  23 * Description: Linux device driver for LSI Logic MegaRAID controller
  24 *
  25 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  26 *                                      518, 520, 531, 532
  27 *
  28 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  29 * and others. Please send updates to the mailing list
  30 * linux-scsi@vger.kernel.org .
  31 *
  32 */
  33
  34#include <linux/mm.h>
  35#include <linux/fs.h>
  36#include <linux/blkdev.h>
  37#include <asm/uaccess.h>
  38#include <asm/io.h>
  39#include <linux/completion.h>
  40#include <linux/delay.h>
  41#include <linux/proc_fs.h>
  42#include <linux/seq_file.h>
  43#include <linux/reboot.h>
  44#include <linux/module.h>
  45#include <linux/list.h>
  46#include <linux/interrupt.h>
  47#include <linux/pci.h>
  48#include <linux/init.h>
  49#include <linux/dma-mapping.h>
  50#include <linux/mutex.h>
  51#include <linux/slab.h>
  52#include <scsi/scsicam.h>
  53
  54#include "scsi.h"
  55#include <scsi/scsi_host.h>
  56
  57#include "megaraid.h"
  58
  59#define MEGARAID_MODULE_VERSION "2.00.4"
  60
  61MODULE_AUTHOR ("sju@lsil.com");
  62MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
  63MODULE_LICENSE ("GPL");
  64MODULE_VERSION(MEGARAID_MODULE_VERSION);
  65
  66static DEFINE_MUTEX(megadev_mutex);
  67static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  68module_param(max_cmd_per_lun, uint, 0);
  69MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  70
  71static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  72module_param(max_sectors_per_io, ushort, 0);
  73MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  74
  75
  76static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  77module_param(max_mbox_busy_wait, ushort, 0);
  78MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  79
  80#define RDINDOOR(adapter)       readl((adapter)->mmio_base + 0x20)
  81#define RDOUTDOOR(adapter)      readl((adapter)->mmio_base + 0x2C)
  82#define WRINDOOR(adapter,value)  writel(value, (adapter)->mmio_base + 0x20)
  83#define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
  84
  85/*
  86 * Global variables
  87 */
  88
  89static int hba_count;
  90static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  91static struct proc_dir_entry *mega_proc_dir_entry;
  92
  93/* For controller re-ordering */
  94static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  95
  96static long
  97megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
  98
  99/*
 100 * The File Operations structure for the serial/ioctl interface of the driver
 101 */
 102static const struct file_operations megadev_fops = {
 103        .owner          = THIS_MODULE,
 104        .unlocked_ioctl = megadev_unlocked_ioctl,
 105        .open           = megadev_open,
 106        .llseek         = noop_llseek,
 107};
 108
 109/*
 110 * Array to structures for storing the information about the controllers. This
 111 * information is sent to the user level applications, when they do an ioctl
 112 * for this information.
 113 */
 114static struct mcontroller mcontroller[MAX_CONTROLLERS];
 115
 116/* The current driver version */
 117static u32 driver_ver = 0x02000000;
 118
 119/* major number used by the device for character interface */
 120static int major;
 121
 122#define IS_RAID_CH(hba, ch)     (((hba)->mega_ch_class >> (ch)) & 0x01)
 123
 124
 125/*
 126 * Debug variable to print some diagnostic messages
 127 */
 128static int trace_level;
 129
 130/**
 131 * mega_setup_mailbox()
 132 * @adapter - pointer to our soft state
 133 *
 134 * Allocates a 8 byte aligned memory for the handshake mailbox.
 135 */
 136static int
 137mega_setup_mailbox(adapter_t *adapter)
 138{
 139        unsigned long   align;
 140
 141        adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
 142                        sizeof(mbox64_t), &adapter->una_mbox64_dma);
 143
 144        if( !adapter->una_mbox64 ) return -1;
 145                
 146        adapter->mbox = &adapter->una_mbox64->mbox;
 147
 148        adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
 149                        (~0UL ^ 0xFUL));
 150
 151        adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
 152
 153        align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
 154
 155        adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
 156
 157        /*
 158         * Register the mailbox if the controller is an io-mapped controller
 159         */
 160        if( adapter->flag & BOARD_IOMAP ) {
 161
 162                outb(adapter->mbox_dma & 0xFF,
 163                                adapter->host->io_port + MBOX_PORT0);
 164
 165                outb((adapter->mbox_dma >> 8) & 0xFF,
 166                                adapter->host->io_port + MBOX_PORT1);
 167
 168                outb((adapter->mbox_dma >> 16) & 0xFF,
 169                                adapter->host->io_port + MBOX_PORT2);
 170
 171                outb((adapter->mbox_dma >> 24) & 0xFF,
 172                                adapter->host->io_port + MBOX_PORT3);
 173
 174                outb(ENABLE_MBOX_BYTE,
 175                                adapter->host->io_port + ENABLE_MBOX_REGION);
 176
 177                irq_ack(adapter);
 178
 179                irq_enable(adapter);
 180        }
 181
 182        return 0;
 183}
 184
 185
 186/*
 187 * mega_query_adapter()
 188 * @adapter - pointer to our soft state
 189 *
 190 * Issue the adapter inquiry commands to the controller and find out
 191 * information and parameter about the devices attached
 192 */
 193static int
 194mega_query_adapter(adapter_t *adapter)
 195{
 196        dma_addr_t      prod_info_dma_handle;
 197        mega_inquiry3   *inquiry3;
 198        u8      raw_mbox[sizeof(struct mbox_out)];
 199        mbox_t  *mbox;
 200        int     retval;
 201
 202        /* Initialize adapter inquiry mailbox */
 203
 204        mbox = (mbox_t *)raw_mbox;
 205
 206        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 207        memset(&mbox->m_out, 0, sizeof(raw_mbox));
 208
 209        /*
 210         * Try to issue Inquiry3 command
 211         * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
 212         * update enquiry3 structure
 213         */
 214        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 215
 216        inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
 217
 218        raw_mbox[0] = FC_NEW_CONFIG;            /* i.e. mbox->cmd=0xA1 */
 219        raw_mbox[2] = NC_SUBOP_ENQUIRY3;        /* i.e. 0x0F */
 220        raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;  /* i.e. 0x02 */
 221
 222        /* Issue a blocking command to the card */
 223        if ((retval = issue_scb_block(adapter, raw_mbox))) {
 224                /* the adapter does not support 40ld */
 225
 226                mraid_ext_inquiry       *ext_inq;
 227                mraid_inquiry           *inq;
 228                dma_addr_t              dma_handle;
 229
 230                ext_inq = pci_alloc_consistent(adapter->dev,
 231                                sizeof(mraid_ext_inquiry), &dma_handle);
 232
 233                if( ext_inq == NULL ) return -1;
 234
 235                inq = &ext_inq->raid_inq;
 236
 237                mbox->m_out.xferaddr = (u32)dma_handle;
 238
 239                /*issue old 0x04 command to adapter */
 240                mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
 241
 242                issue_scb_block(adapter, raw_mbox);
 243
 244                /*
 245                 * update Enquiry3 and ProductInfo structures with
 246                 * mraid_inquiry structure
 247                 */
 248                mega_8_to_40ld(inq, inquiry3,
 249                                (mega_product_info *)&adapter->product_info);
 250
 251                pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
 252                                ext_inq, dma_handle);
 253
 254        } else {                /*adapter supports 40ld */
 255                adapter->flag |= BOARD_40LD;
 256
 257                /*
 258                 * get product_info, which is static information and will be
 259                 * unchanged
 260                 */
 261                prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
 262                                &adapter->product_info,
 263                                sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
 264
 265                mbox->m_out.xferaddr = prod_info_dma_handle;
 266
 267                raw_mbox[0] = FC_NEW_CONFIG;    /* i.e. mbox->cmd=0xA1 */
 268                raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;    /* i.e. 0x0E */
 269
 270                if ((retval = issue_scb_block(adapter, raw_mbox)))
 271                        printk(KERN_WARNING
 272                        "megaraid: Product_info cmd failed with error: %d\n",
 273                                retval);
 274
 275                pci_unmap_single(adapter->dev, prod_info_dma_handle,
 276                                sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
 277        }
 278
 279
 280        /*
 281         * kernel scans the channels from 0 to <= max_channel
 282         */
 283        adapter->host->max_channel =
 284                adapter->product_info.nchannels + NVIRT_CHAN -1;
 285
 286        adapter->host->max_id = 16;     /* max targets per channel */
 287
 288        adapter->host->max_lun = 7;     /* Up to 7 luns for non disk devices */
 289
 290        adapter->host->cmd_per_lun = max_cmd_per_lun;
 291
 292        adapter->numldrv = inquiry3->num_ldrv;
 293
 294        adapter->max_cmds = adapter->product_info.max_commands;
 295
 296        if(adapter->max_cmds > MAX_COMMANDS)
 297                adapter->max_cmds = MAX_COMMANDS;
 298
 299        adapter->host->can_queue = adapter->max_cmds - 1;
 300
 301        /*
 302         * Get the maximum number of scatter-gather elements supported by this
 303         * firmware
 304         */
 305        mega_get_max_sgl(adapter);
 306
 307        adapter->host->sg_tablesize = adapter->sglen;
 308
 309        /* use HP firmware and bios version encoding
 310           Note: fw_version[0|1] and bios_version[0|1] were originally shifted
 311           right 8 bits making them zero. This 0 value was hardcoded to fix
 312           sparse warnings. */
 313        if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
 314                sprintf (adapter->fw_version, "%c%d%d.%d%d",
 315                         adapter->product_info.fw_version[2],
 316                         0,
 317                         adapter->product_info.fw_version[1] & 0x0f,
 318                         0,
 319                         adapter->product_info.fw_version[0] & 0x0f);
 320                sprintf (adapter->bios_version, "%c%d%d.%d%d",
 321                         adapter->product_info.bios_version[2],
 322                         0,
 323                         adapter->product_info.bios_version[1] & 0x0f,
 324                         0,
 325                         adapter->product_info.bios_version[0] & 0x0f);
 326        } else {
 327                memcpy(adapter->fw_version,
 328                                (char *)adapter->product_info.fw_version, 4);
 329                adapter->fw_version[4] = 0;
 330
 331                memcpy(adapter->bios_version,
 332                                (char *)adapter->product_info.bios_version, 4);
 333
 334                adapter->bios_version[4] = 0;
 335        }
 336
 337        printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
 338                adapter->fw_version, adapter->bios_version, adapter->numldrv);
 339
 340        /*
 341         * Do we support extended (>10 bytes) cdbs
 342         */
 343        adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
 344        if (adapter->support_ext_cdb)
 345                printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
 346
 347
 348        return 0;
 349}
 350
 351/**
 352 * mega_runpendq()
 353 * @adapter - pointer to our soft state
 354 *
 355 * Runs through the list of pending requests.
 356 */
 357static inline void
 358mega_runpendq(adapter_t *adapter)
 359{
 360        if(!list_empty(&adapter->pending_list))
 361                __mega_runpendq(adapter);
 362}
 363
 364/*
 365 * megaraid_queue()
 366 * @scmd - Issue this scsi command
 367 * @done - the callback hook into the scsi mid-layer
 368 *
 369 * The command queuing entry point for the mid-layer.
 370 */
 371static int
 372megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
 373{
 374        adapter_t       *adapter;
 375        scb_t   *scb;
 376        int     busy=0;
 377        unsigned long flags;
 378
 379        adapter = (adapter_t *)scmd->device->host->hostdata;
 380
 381        scmd->scsi_done = done;
 382
 383
 384        /*
 385         * Allocate and build a SCB request
 386         * busy flag will be set if mega_build_cmd() command could not
 387         * allocate scb. We will return non-zero status in that case.
 388         * NOTE: scb can be null even though certain commands completed
 389         * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
 390         * return 0 in that case.
 391         */
 392
 393        spin_lock_irqsave(&adapter->lock, flags);
 394        scb = mega_build_cmd(adapter, scmd, &busy);
 395        if (!scb)
 396                goto out;
 397
 398        scb->state |= SCB_PENDQ;
 399        list_add_tail(&scb->list, &adapter->pending_list);
 400
 401        /*
 402         * Check if the HBA is in quiescent state, e.g., during a
 403         * delete logical drive opertion. If it is, don't run
 404         * the pending_list.
 405         */
 406        if (atomic_read(&adapter->quiescent) == 0)
 407                mega_runpendq(adapter);
 408
 409        busy = 0;
 410 out:
 411        spin_unlock_irqrestore(&adapter->lock, flags);
 412        return busy;
 413}
 414
 415static DEF_SCSI_QCMD(megaraid_queue)
 416
 417/**
 418 * mega_allocate_scb()
 419 * @adapter - pointer to our soft state
 420 * @cmd - scsi command from the mid-layer
 421 *
 422 * Allocate a SCB structure. This is the central structure for controller
 423 * commands.
 424 */
 425static inline scb_t *
 426mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
 427{
 428        struct list_head *head = &adapter->free_list;
 429        scb_t   *scb;
 430
 431        /* Unlink command from Free List */
 432        if( !list_empty(head) ) {
 433
 434                scb = list_entry(head->next, scb_t, list);
 435
 436                list_del_init(head->next);
 437
 438                scb->state = SCB_ACTIVE;
 439                scb->cmd = cmd;
 440                scb->dma_type = MEGA_DMA_TYPE_NONE;
 441
 442                return scb;
 443        }
 444
 445        return NULL;
 446}
 447
 448/**
 449 * mega_get_ldrv_num()
 450 * @adapter - pointer to our soft state
 451 * @cmd - scsi mid layer command
 452 * @channel - channel on the controller
 453 *
 454 * Calculate the logical drive number based on the information in scsi command
 455 * and the channel number.
 456 */
 457static inline int
 458mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
 459{
 460        int             tgt;
 461        int             ldrv_num;
 462
 463        tgt = cmd->device->id;
 464        
 465        if ( tgt > adapter->this_id )
 466                tgt--;  /* we do not get inquires for initiator id */
 467
 468        ldrv_num = (channel * 15) + tgt;
 469
 470
 471        /*
 472         * If we have a logical drive with boot enabled, project it first
 473         */
 474        if( adapter->boot_ldrv_enabled ) {
 475                if( ldrv_num == 0 ) {
 476                        ldrv_num = adapter->boot_ldrv;
 477                }
 478                else {
 479                        if( ldrv_num <= adapter->boot_ldrv ) {
 480                                ldrv_num--;
 481                        }
 482                }
 483        }
 484
 485        /*
 486         * If "delete logical drive" feature is enabled on this controller.
 487         * Do only if at least one delete logical drive operation was done.
 488         *
 489         * Also, after logical drive deletion, instead of logical drive number,
 490         * the value returned should be 0x80+logical drive id.
 491         *
 492         * These is valid only for IO commands.
 493         */
 494
 495        if (adapter->support_random_del && adapter->read_ldidmap )
 496                switch (cmd->cmnd[0]) {
 497                case READ_6:    /* fall through */
 498                case WRITE_6:   /* fall through */
 499                case READ_10:   /* fall through */
 500                case WRITE_10:
 501                        ldrv_num += 0x80;
 502                }
 503
 504        return ldrv_num;
 505}
 506
 507/**
 508 * mega_build_cmd()
 509 * @adapter - pointer to our soft state
 510 * @cmd - Prepare using this scsi command
 511 * @busy - busy flag if no resources
 512 *
 513 * Prepares a command and scatter gather list for the controller. This routine
 514 * also finds out if the commands is intended for a logical drive or a
 515 * physical device and prepares the controller command accordingly.
 516 *
 517 * We also re-order the logical drives and physical devices based on their
 518 * boot settings.
 519 */
 520static scb_t *
 521mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
 522{
 523        mega_ext_passthru       *epthru;
 524        mega_passthru   *pthru;
 525        scb_t   *scb;
 526        mbox_t  *mbox;
 527        u32     seg;
 528        char    islogical;
 529        int     max_ldrv_num;
 530        int     channel = 0;
 531        int     target = 0;
 532        int     ldrv_num = 0;   /* logical drive number */
 533
 534
 535        /*
 536         * filter the internal and ioctl commands
 537         */
 538        if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
 539                return (scb_t *)cmd->host_scribble;
 540
 541        /*
 542         * We know what channels our logical drives are on - mega_find_card()
 543         */
 544        islogical = adapter->logdrv_chan[cmd->device->channel];
 545
 546        /*
 547         * The theory: If physical drive is chosen for boot, all the physical
 548         * devices are exported before the logical drives, otherwise physical
 549         * devices are pushed after logical drives, in which case - Kernel sees
 550         * the physical devices on virtual channel which is obviously converted
 551         * to actual channel on the HBA.
 552         */
 553        if( adapter->boot_pdrv_enabled ) {
 554                if( islogical ) {
 555                        /* logical channel */
 556                        channel = cmd->device->channel -
 557                                adapter->product_info.nchannels;
 558                }
 559                else {
 560                        /* this is physical channel */
 561                        channel = cmd->device->channel; 
 562                        target = cmd->device->id;
 563
 564                        /*
 565                         * boot from a physical disk, that disk needs to be
 566                         * exposed first IF both the channels are SCSI, then
 567                         * booting from the second channel is not allowed.
 568                         */
 569                        if( target == 0 ) {
 570                                target = adapter->boot_pdrv_tgt;
 571                        }
 572                        else if( target == adapter->boot_pdrv_tgt ) {
 573                                target = 0;
 574                        }
 575                }
 576        }
 577        else {
 578                if( islogical ) {
 579                        /* this is the logical channel */
 580                        channel = cmd->device->channel; 
 581                }
 582                else {
 583                        /* physical channel */
 584                        channel = cmd->device->channel - NVIRT_CHAN;    
 585                        target = cmd->device->id;
 586                }
 587        }
 588
 589
 590        if(islogical) {
 591
 592                /* have just LUN 0 for each target on virtual channels */
 593                if (cmd->device->lun) {
 594                        cmd->result = (DID_BAD_TARGET << 16);
 595                        cmd->scsi_done(cmd);
 596                        return NULL;
 597                }
 598
 599                ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
 600
 601
 602                max_ldrv_num = (adapter->flag & BOARD_40LD) ?
 603                        MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
 604
 605                /*
 606                 * max_ldrv_num increases by 0x80 if some logical drive was
 607                 * deleted.
 608                 */
 609                if(adapter->read_ldidmap)
 610                        max_ldrv_num += 0x80;
 611
 612                if(ldrv_num > max_ldrv_num ) {
 613                        cmd->result = (DID_BAD_TARGET << 16);
 614                        cmd->scsi_done(cmd);
 615                        return NULL;
 616                }
 617
 618        }
 619        else {
 620                if( cmd->device->lun > 7) {
 621                        /*
 622                         * Do not support lun >7 for physically accessed
 623                         * devices
 624                         */
 625                        cmd->result = (DID_BAD_TARGET << 16);
 626                        cmd->scsi_done(cmd);
 627                        return NULL;
 628                }
 629        }
 630
 631        /*
 632         *
 633         * Logical drive commands
 634         *
 635         */
 636        if(islogical) {
 637                switch (cmd->cmnd[0]) {
 638                case TEST_UNIT_READY:
 639#if MEGA_HAVE_CLUSTERING
 640                        /*
 641                         * Do we support clustering and is the support enabled
 642                         * If no, return success always
 643                         */
 644                        if( !adapter->has_cluster ) {
 645                                cmd->result = (DID_OK << 16);
 646                                cmd->scsi_done(cmd);
 647                                return NULL;
 648                        }
 649
 650                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
 651                                *busy = 1;
 652                                return NULL;
 653                        }
 654
 655                        scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
 656                        scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
 657                        scb->raw_mbox[3] = ldrv_num;
 658
 659                        scb->dma_direction = PCI_DMA_NONE;
 660
 661                        return scb;
 662#else
 663                        cmd->result = (DID_OK << 16);
 664                        cmd->scsi_done(cmd);
 665                        return NULL;
 666#endif
 667
 668                case MODE_SENSE: {
 669                        char *buf;
 670                        struct scatterlist *sg;
 671
 672                        sg = scsi_sglist(cmd);
 673                        buf = kmap_atomic(sg_page(sg)) + sg->offset;
 674
 675                        memset(buf, 0, cmd->cmnd[4]);
 676                        kunmap_atomic(buf - sg->offset);
 677
 678                        cmd->result = (DID_OK << 16);
 679                        cmd->scsi_done(cmd);
 680                        return NULL;
 681                }
 682
 683                case READ_CAPACITY:
 684                case INQUIRY:
 685
 686                        if(!(adapter->flag & (1L << cmd->device->channel))) {
 687
 688                                printk(KERN_NOTICE
 689                                        "scsi%d: scanning scsi channel %d ",
 690                                                adapter->host->host_no,
 691                                                cmd->device->channel);
 692                                printk("for logical drives.\n");
 693
 694                                adapter->flag |= (1L << cmd->device->channel);
 695                        }
 696
 697                        /* Allocate a SCB and initialize passthru */
 698                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
 699                                *busy = 1;
 700                                return NULL;
 701                        }
 702                        pthru = scb->pthru;
 703
 704                        mbox = (mbox_t *)scb->raw_mbox;
 705                        memset(mbox, 0, sizeof(scb->raw_mbox));
 706                        memset(pthru, 0, sizeof(mega_passthru));
 707
 708                        pthru->timeout = 0;
 709                        pthru->ars = 1;
 710                        pthru->reqsenselen = 14;
 711                        pthru->islogical = 1;
 712                        pthru->logdrv = ldrv_num;
 713                        pthru->cdblen = cmd->cmd_len;
 714                        memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
 715
 716                        if( adapter->has_64bit_addr ) {
 717                                mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
 718                        }
 719                        else {
 720                                mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
 721                        }
 722
 723                        scb->dma_direction = PCI_DMA_FROMDEVICE;
 724
 725                        pthru->numsgelements = mega_build_sglist(adapter, scb,
 726                                &pthru->dataxferaddr, &pthru->dataxferlen);
 727
 728                        mbox->m_out.xferaddr = scb->pthru_dma_addr;
 729
 730                        return scb;
 731
 732                case READ_6:
 733                case WRITE_6:
 734                case READ_10:
 735                case WRITE_10:
 736                case READ_12:
 737                case WRITE_12:
 738
 739                        /* Allocate a SCB and initialize mailbox */
 740                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
 741                                *busy = 1;
 742                                return NULL;
 743                        }
 744                        mbox = (mbox_t *)scb->raw_mbox;
 745
 746                        memset(mbox, 0, sizeof(scb->raw_mbox));
 747                        mbox->m_out.logdrv = ldrv_num;
 748
 749                        /*
 750                         * A little hack: 2nd bit is zero for all scsi read
 751                         * commands and is set for all scsi write commands
 752                         */
 753                        if( adapter->has_64bit_addr ) {
 754                                mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
 755                                        MEGA_MBOXCMD_LWRITE64:
 756                                        MEGA_MBOXCMD_LREAD64 ;
 757                        }
 758                        else {
 759                                mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
 760                                        MEGA_MBOXCMD_LWRITE:
 761                                        MEGA_MBOXCMD_LREAD ;
 762                        }
 763
 764                        /*
 765                         * 6-byte READ(0x08) or WRITE(0x0A) cdb
 766                         */
 767                        if( cmd->cmd_len == 6 ) {
 768                                mbox->m_out.numsectors = (u32) cmd->cmnd[4];
 769                                mbox->m_out.lba =
 770                                        ((u32)cmd->cmnd[1] << 16) |
 771                                        ((u32)cmd->cmnd[2] << 8) |
 772                                        (u32)cmd->cmnd[3];
 773
 774                                mbox->m_out.lba &= 0x1FFFFF;
 775
 776#if MEGA_HAVE_STATS
 777                                /*
 778                                 * Take modulo 0x80, since the logical drive
 779                                 * number increases by 0x80 when a logical
 780                                 * drive was deleted
 781                                 */
 782                                if (*cmd->cmnd == READ_6) {
 783                                        adapter->nreads[ldrv_num%0x80]++;
 784                                        adapter->nreadblocks[ldrv_num%0x80] +=
 785                                                mbox->m_out.numsectors;
 786                                } else {
 787                                        adapter->nwrites[ldrv_num%0x80]++;
 788                                        adapter->nwriteblocks[ldrv_num%0x80] +=
 789                                                mbox->m_out.numsectors;
 790                                }
 791#endif
 792                        }
 793
 794                        /*
 795                         * 10-byte READ(0x28) or WRITE(0x2A) cdb
 796                         */
 797                        if( cmd->cmd_len == 10 ) {
 798                                mbox->m_out.numsectors =
 799                                        (u32)cmd->cmnd[8] |
 800                                        ((u32)cmd->cmnd[7] << 8);
 801                                mbox->m_out.lba =
 802                                        ((u32)cmd->cmnd[2] << 24) |
 803                                        ((u32)cmd->cmnd[3] << 16) |
 804                                        ((u32)cmd->cmnd[4] << 8) |
 805                                        (u32)cmd->cmnd[5];
 806
 807#if MEGA_HAVE_STATS
 808                                if (*cmd->cmnd == READ_10) {
 809                                        adapter->nreads[ldrv_num%0x80]++;
 810                                        adapter->nreadblocks[ldrv_num%0x80] +=
 811                                                mbox->m_out.numsectors;
 812                                } else {
 813                                        adapter->nwrites[ldrv_num%0x80]++;
 814                                        adapter->nwriteblocks[ldrv_num%0x80] +=
 815                                                mbox->m_out.numsectors;
 816                                }
 817#endif
 818                        }
 819
 820                        /*
 821                         * 12-byte READ(0xA8) or WRITE(0xAA) cdb
 822                         */
 823                        if( cmd->cmd_len == 12 ) {
 824                                mbox->m_out.lba =
 825                                        ((u32)cmd->cmnd[2] << 24) |
 826                                        ((u32)cmd->cmnd[3] << 16) |
 827                                        ((u32)cmd->cmnd[4] << 8) |
 828                                        (u32)cmd->cmnd[5];
 829
 830                                mbox->m_out.numsectors =
 831                                        ((u32)cmd->cmnd[6] << 24) |
 832                                        ((u32)cmd->cmnd[7] << 16) |
 833                                        ((u32)cmd->cmnd[8] << 8) |
 834                                        (u32)cmd->cmnd[9];
 835
 836#if MEGA_HAVE_STATS
 837                                if (*cmd->cmnd == READ_12) {
 838                                        adapter->nreads[ldrv_num%0x80]++;
 839                                        adapter->nreadblocks[ldrv_num%0x80] +=
 840                                                mbox->m_out.numsectors;
 841                                } else {
 842                                        adapter->nwrites[ldrv_num%0x80]++;
 843                                        adapter->nwriteblocks[ldrv_num%0x80] +=
 844                                                mbox->m_out.numsectors;
 845                                }
 846#endif
 847                        }
 848
 849                        /*
 850                         * If it is a read command
 851                         */
 852                        if( (*cmd->cmnd & 0x0F) == 0x08 ) {
 853                                scb->dma_direction = PCI_DMA_FROMDEVICE;
 854                        }
 855                        else {
 856                                scb->dma_direction = PCI_DMA_TODEVICE;
 857                        }
 858
 859                        /* Calculate Scatter-Gather info */
 860                        mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
 861                                        (u32 *)&mbox->m_out.xferaddr, &seg);
 862
 863                        return scb;
 864
 865#if MEGA_HAVE_CLUSTERING
 866                case RESERVE:   /* Fall through */
 867                case RELEASE:
 868
 869                        /*
 870                         * Do we support clustering and is the support enabled
 871                         */
 872                        if( ! adapter->has_cluster ) {
 873
 874                                cmd->result = (DID_BAD_TARGET << 16);
 875                                cmd->scsi_done(cmd);
 876                                return NULL;
 877                        }
 878
 879                        /* Allocate a SCB and initialize mailbox */
 880                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
 881                                *busy = 1;
 882                                return NULL;
 883                        }
 884
 885                        scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
 886                        scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
 887                                MEGA_RESERVE_LD : MEGA_RELEASE_LD;
 888
 889                        scb->raw_mbox[3] = ldrv_num;
 890
 891                        scb->dma_direction = PCI_DMA_NONE;
 892
 893                        return scb;
 894#endif
 895
 896                default:
 897                        cmd->result = (DID_BAD_TARGET << 16);
 898                        cmd->scsi_done(cmd);
 899                        return NULL;
 900                }
 901        }
 902
 903        /*
 904         * Passthru drive commands
 905         */
 906        else {
 907                /* Allocate a SCB and initialize passthru */
 908                if(!(scb = mega_allocate_scb(adapter, cmd))) {
 909                        *busy = 1;
 910                        return NULL;
 911                }
 912
 913                mbox = (mbox_t *)scb->raw_mbox;
 914                memset(mbox, 0, sizeof(scb->raw_mbox));
 915
 916                if( adapter->support_ext_cdb ) {
 917
 918                        epthru = mega_prepare_extpassthru(adapter, scb, cmd,
 919                                        channel, target);
 920
 921                        mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
 922
 923                        mbox->m_out.xferaddr = scb->epthru_dma_addr;
 924
 925                }
 926                else {
 927
 928                        pthru = mega_prepare_passthru(adapter, scb, cmd,
 929                                        channel, target);
 930
 931                        /* Initialize mailbox */
 932                        if( adapter->has_64bit_addr ) {
 933                                mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
 934                        }
 935                        else {
 936                                mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
 937                        }
 938
 939                        mbox->m_out.xferaddr = scb->pthru_dma_addr;
 940
 941                }
 942                return scb;
 943        }
 944        return NULL;
 945}
 946
 947
 948/**
 949 * mega_prepare_passthru()
 950 * @adapter - pointer to our soft state
 951 * @scb - our scsi control block
 952 * @cmd - scsi command from the mid-layer
 953 * @channel - actual channel on the controller
 954 * @target - actual id on the controller.
 955 *
 956 * prepare a command for the scsi physical devices.
 957 */
 958static mega_passthru *
 959mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
 960                int channel, int target)
 961{
 962        mega_passthru *pthru;
 963
 964        pthru = scb->pthru;
 965        memset(pthru, 0, sizeof (mega_passthru));
 966
 967        /* 0=6sec/1=60sec/2=10min/3=3hrs */
 968        pthru->timeout = 2;
 969
 970        pthru->ars = 1;
 971        pthru->reqsenselen = 14;
 972        pthru->islogical = 0;
 973
 974        pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
 975
 976        pthru->target = (adapter->flag & BOARD_40LD) ?
 977                (channel << 4) | target : target;
 978
 979        pthru->cdblen = cmd->cmd_len;
 980        pthru->logdrv = cmd->device->lun;
 981
 982        memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
 983
 984        /* Not sure about the direction */
 985        scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
 986
 987        /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
 988        switch (cmd->cmnd[0]) {
 989        case INQUIRY:
 990        case READ_CAPACITY:
 991                if(!(adapter->flag & (1L << cmd->device->channel))) {
 992
 993                        printk(KERN_NOTICE
 994                                "scsi%d: scanning scsi channel %d [P%d] ",
 995                                        adapter->host->host_no,
 996                                        cmd->device->channel, channel);
 997                        printk("for physical devices.\n");
 998
 999                        adapter->flag |= (1L << cmd->device->channel);
1000                }
1001                /* Fall through */
1002        default:
1003                pthru->numsgelements = mega_build_sglist(adapter, scb,
1004                                &pthru->dataxferaddr, &pthru->dataxferlen);
1005                break;
1006        }
1007        return pthru;
1008}
1009
1010
1011/**
1012 * mega_prepare_extpassthru()
1013 * @adapter - pointer to our soft state
1014 * @scb - our scsi control block
1015 * @cmd - scsi command from the mid-layer
1016 * @channel - actual channel on the controller
1017 * @target - actual id on the controller.
1018 *
1019 * prepare a command for the scsi physical devices. This rountine prepares
1020 * commands for devices which can take extended CDBs (>10 bytes)
1021 */
1022static mega_ext_passthru *
1023mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1024                int channel, int target)
1025{
1026        mega_ext_passthru       *epthru;
1027
1028        epthru = scb->epthru;
1029        memset(epthru, 0, sizeof(mega_ext_passthru));
1030
1031        /* 0=6sec/1=60sec/2=10min/3=3hrs */
1032        epthru->timeout = 2;
1033
1034        epthru->ars = 1;
1035        epthru->reqsenselen = 14;
1036        epthru->islogical = 0;
1037
1038        epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1039        epthru->target = (adapter->flag & BOARD_40LD) ?
1040                (channel << 4) | target : target;
1041
1042        epthru->cdblen = cmd->cmd_len;
1043        epthru->logdrv = cmd->device->lun;
1044
1045        memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1046
1047        /* Not sure about the direction */
1048        scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1049
1050        switch(cmd->cmnd[0]) {
1051        case INQUIRY:
1052        case READ_CAPACITY:
1053                if(!(adapter->flag & (1L << cmd->device->channel))) {
1054
1055                        printk(KERN_NOTICE
1056                                "scsi%d: scanning scsi channel %d [P%d] ",
1057                                        adapter->host->host_no,
1058                                        cmd->device->channel, channel);
1059                        printk("for physical devices.\n");
1060
1061                        adapter->flag |= (1L << cmd->device->channel);
1062                }
1063                /* Fall through */
1064        default:
1065                epthru->numsgelements = mega_build_sglist(adapter, scb,
1066                                &epthru->dataxferaddr, &epthru->dataxferlen);
1067                break;
1068        }
1069
1070        return epthru;
1071}
1072
1073static void
1074__mega_runpendq(adapter_t *adapter)
1075{
1076        scb_t *scb;
1077        struct list_head *pos, *next;
1078
1079        /* Issue any pending commands to the card */
1080        list_for_each_safe(pos, next, &adapter->pending_list) {
1081
1082                scb = list_entry(pos, scb_t, list);
1083
1084                if( !(scb->state & SCB_ISSUED) ) {
1085
1086                        if( issue_scb(adapter, scb) != 0 )
1087                                return;
1088                }
1089        }
1090
1091        return;
1092}
1093
1094
1095/**
1096 * issue_scb()
1097 * @adapter - pointer to our soft state
1098 * @scb - scsi control block
1099 *
1100 * Post a command to the card if the mailbox is available, otherwise return
1101 * busy. We also take the scb from the pending list if the mailbox is
1102 * available.
1103 */
1104static int
1105issue_scb(adapter_t *adapter, scb_t *scb)
1106{
1107        volatile mbox64_t       *mbox64 = adapter->mbox64;
1108        volatile mbox_t         *mbox = adapter->mbox;
1109        unsigned int    i = 0;
1110
1111        if(unlikely(mbox->m_in.busy)) {
1112                do {
1113                        udelay(1);
1114                        i++;
1115                } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1116
1117                if(mbox->m_in.busy) return -1;
1118        }
1119
1120        /* Copy mailbox data into host structure */
1121        memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox, 
1122                        sizeof(struct mbox_out));
1123
1124        mbox->m_out.cmdid = scb->idx;   /* Set cmdid */
1125        mbox->m_in.busy = 1;            /* Set busy */
1126
1127
1128        /*
1129         * Increment the pending queue counter
1130         */
1131        atomic_inc(&adapter->pend_cmds);
1132
1133        switch (mbox->m_out.cmd) {
1134        case MEGA_MBOXCMD_LREAD64:
1135        case MEGA_MBOXCMD_LWRITE64:
1136        case MEGA_MBOXCMD_PASSTHRU64:
1137        case MEGA_MBOXCMD_EXTPTHRU:
1138                mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1139                mbox64->xfer_segment_hi = 0;
1140                mbox->m_out.xferaddr = 0xFFFFFFFF;
1141                break;
1142        default:
1143                mbox64->xfer_segment_lo = 0;
1144                mbox64->xfer_segment_hi = 0;
1145        }
1146
1147        /*
1148         * post the command
1149         */
1150        scb->state |= SCB_ISSUED;
1151
1152        if( likely(adapter->flag & BOARD_MEMMAP) ) {
1153                mbox->m_in.poll = 0;
1154                mbox->m_in.ack = 0;
1155                WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1156        }
1157        else {
1158                irq_enable(adapter);
1159                issue_command(adapter);
1160        }
1161
1162        return 0;
1163}
1164
1165/*
1166 * Wait until the controller's mailbox is available
1167 */
1168static inline int
1169mega_busywait_mbox (adapter_t *adapter)
1170{
1171        if (adapter->mbox->m_in.busy)
1172                return __mega_busywait_mbox(adapter);
1173        return 0;
1174}
1175
1176/**
1177 * issue_scb_block()
1178 * @adapter - pointer to our soft state
1179 * @raw_mbox - the mailbox
1180 *
1181 * Issue a scb in synchronous and non-interrupt mode
1182 */
1183static int
1184issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1185{
1186        volatile mbox64_t *mbox64 = adapter->mbox64;
1187        volatile mbox_t *mbox = adapter->mbox;
1188        u8      byte;
1189
1190        /* Wait until mailbox is free */
1191        if(mega_busywait_mbox (adapter))
1192                goto bug_blocked_mailbox;
1193
1194        /* Copy mailbox data into host structure */
1195        memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1196        mbox->m_out.cmdid = 0xFE;
1197        mbox->m_in.busy = 1;
1198
1199        switch (raw_mbox[0]) {
1200        case MEGA_MBOXCMD_LREAD64:
1201        case MEGA_MBOXCMD_LWRITE64:
1202        case MEGA_MBOXCMD_PASSTHRU64:
1203        case MEGA_MBOXCMD_EXTPTHRU:
1204                mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1205                mbox64->xfer_segment_hi = 0;
1206                mbox->m_out.xferaddr = 0xFFFFFFFF;
1207                break;
1208        default:
1209                mbox64->xfer_segment_lo = 0;
1210                mbox64->xfer_segment_hi = 0;
1211        }
1212
1213        if( likely(adapter->flag & BOARD_MEMMAP) ) {
1214                mbox->m_in.poll = 0;
1215                mbox->m_in.ack = 0;
1216                mbox->m_in.numstatus = 0xFF;
1217                mbox->m_in.status = 0xFF;
1218                WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1219
1220                while((volatile u8)mbox->m_in.numstatus == 0xFF)
1221                        cpu_relax();
1222
1223                mbox->m_in.numstatus = 0xFF;
1224
1225                while( (volatile u8)mbox->m_in.poll != 0x77 )
1226                        cpu_relax();
1227
1228                mbox->m_in.poll = 0;
1229                mbox->m_in.ack = 0x77;
1230
1231                WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1232
1233                while(RDINDOOR(adapter) & 0x2)
1234                        cpu_relax();
1235        }
1236        else {
1237                irq_disable(adapter);
1238                issue_command(adapter);
1239
1240                while (!((byte = irq_state(adapter)) & INTR_VALID))
1241                        cpu_relax();
1242
1243                set_irq_state(adapter, byte);
1244                irq_enable(adapter);
1245                irq_ack(adapter);
1246        }
1247
1248        return mbox->m_in.status;
1249
1250bug_blocked_mailbox:
1251        printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1252        udelay (1000);
1253        return -1;
1254}
1255
1256
1257/**
1258 * megaraid_isr_iomapped()
1259 * @irq - irq
1260 * @devp - pointer to our soft state
1261 *
1262 * Interrupt service routine for io-mapped controllers.
1263 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1264 * and service the completed commands.
1265 */
1266static irqreturn_t
1267megaraid_isr_iomapped(int irq, void *devp)
1268{
1269        adapter_t       *adapter = devp;
1270        unsigned long   flags;
1271        u8      status;
1272        u8      nstatus;
1273        u8      completed[MAX_FIRMWARE_STATUS];
1274        u8      byte;
1275        int     handled = 0;
1276
1277
1278        /*
1279         * loop till F/W has more commands for us to complete.
1280         */
1281        spin_lock_irqsave(&adapter->lock, flags);
1282
1283        do {
1284                /* Check if a valid interrupt is pending */
1285                byte = irq_state(adapter);
1286                if( (byte & VALID_INTR_BYTE) == 0 ) {
1287                        /*
1288                         * No more pending commands
1289                         */
1290                        goto out_unlock;
1291                }
1292                set_irq_state(adapter, byte);
1293
1294                while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1295                                == 0xFF)
1296                        cpu_relax();
1297                adapter->mbox->m_in.numstatus = 0xFF;
1298
1299                status = adapter->mbox->m_in.status;
1300
1301                /*
1302                 * decrement the pending queue counter
1303                 */
1304                atomic_sub(nstatus, &adapter->pend_cmds);
1305
1306                memcpy(completed, (void *)adapter->mbox->m_in.completed, 
1307                                nstatus);
1308
1309                /* Acknowledge interrupt */
1310                irq_ack(adapter);
1311
1312                mega_cmd_done(adapter, completed, nstatus, status);
1313
1314                mega_rundoneq(adapter);
1315
1316                handled = 1;
1317
1318                /* Loop through any pending requests */
1319                if(atomic_read(&adapter->quiescent) == 0) {
1320                        mega_runpendq(adapter);
1321                }
1322
1323        } while(1);
1324
1325 out_unlock:
1326
1327        spin_unlock_irqrestore(&adapter->lock, flags);
1328
1329        return IRQ_RETVAL(handled);
1330}
1331
1332
1333/**
1334 * megaraid_isr_memmapped()
1335 * @irq - irq
1336 * @devp - pointer to our soft state
1337 *
1338 * Interrupt service routine for memory-mapped controllers.
1339 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1340 * and service the completed commands.
1341 */
1342static irqreturn_t
1343megaraid_isr_memmapped(int irq, void *devp)
1344{
1345        adapter_t       *adapter = devp;
1346        unsigned long   flags;
1347        u8      status;
1348        u32     dword = 0;
1349        u8      nstatus;
1350        u8      completed[MAX_FIRMWARE_STATUS];
1351        int     handled = 0;
1352
1353
1354        /*
1355         * loop till F/W has more commands for us to complete.
1356         */
1357        spin_lock_irqsave(&adapter->lock, flags);
1358
1359        do {
1360                /* Check if a valid interrupt is pending */
1361                dword = RDOUTDOOR(adapter);
1362                if(dword != 0x10001234) {
1363                        /*
1364                         * No more pending commands
1365                         */
1366                        goto out_unlock;
1367                }
1368                WROUTDOOR(adapter, 0x10001234);
1369
1370                while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1371                                == 0xFF) {
1372                        cpu_relax();
1373                }
1374                adapter->mbox->m_in.numstatus = 0xFF;
1375
1376                status = adapter->mbox->m_in.status;
1377
1378                /*
1379                 * decrement the pending queue counter
1380                 */
1381                atomic_sub(nstatus, &adapter->pend_cmds);
1382
1383                memcpy(completed, (void *)adapter->mbox->m_in.completed, 
1384                                nstatus);
1385
1386                /* Acknowledge interrupt */
1387                WRINDOOR(adapter, 0x2);
1388
1389                handled = 1;
1390
1391                while( RDINDOOR(adapter) & 0x02 )
1392                        cpu_relax();
1393
1394                mega_cmd_done(adapter, completed, nstatus, status);
1395
1396                mega_rundoneq(adapter);
1397
1398                /* Loop through any pending requests */
1399                if(atomic_read(&adapter->quiescent) == 0) {
1400                        mega_runpendq(adapter);
1401                }
1402
1403        } while(1);
1404
1405 out_unlock:
1406
1407        spin_unlock_irqrestore(&adapter->lock, flags);
1408
1409        return IRQ_RETVAL(handled);
1410}
1411/**
1412 * mega_cmd_done()
1413 * @adapter - pointer to our soft state
1414 * @completed - array of ids of completed commands
1415 * @nstatus - number of completed commands
1416 * @status - status of the last command completed
1417 *
1418 * Complete the commands and call the scsi mid-layer callback hooks.
1419 */
1420static void
1421mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1422{
1423        mega_ext_passthru       *epthru = NULL;
1424        struct scatterlist      *sgl;
1425        Scsi_Cmnd       *cmd = NULL;
1426        mega_passthru   *pthru = NULL;
1427        mbox_t  *mbox = NULL;
1428        u8      c;
1429        scb_t   *scb;
1430        int     islogical;
1431        int     cmdid;
1432        int     i;
1433
1434        /*
1435         * for all the commands completed, call the mid-layer callback routine
1436         * and free the scb.
1437         */
1438        for( i = 0; i < nstatus; i++ ) {
1439
1440                cmdid = completed[i];
1441
1442                if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1443                        scb = &adapter->int_scb;
1444                        cmd = scb->cmd;
1445                        mbox = (mbox_t *)scb->raw_mbox;
1446
1447                        /*
1448                         * Internal command interface do not fire the extended
1449                         * passthru or 64-bit passthru
1450                         */
1451                        pthru = scb->pthru;
1452
1453                }
1454                else {
1455                        scb = &adapter->scb_list[cmdid];
1456
1457                        /*
1458                         * Make sure f/w has completed a valid command
1459                         */
1460                        if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1461                                printk(KERN_CRIT
1462                                        "megaraid: invalid command ");
1463                                printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1464                                        cmdid, scb->state, scb->cmd);
1465
1466                                continue;
1467                        }
1468
1469                        /*
1470                         * Was a abort issued for this command
1471                         */
1472                        if( scb->state & SCB_ABORT ) {
1473
1474                                printk(KERN_WARNING
1475                                "megaraid: aborted cmd [%x] complete.\n",
1476                                        scb->idx);
1477
1478                                scb->cmd->result = (DID_ABORT << 16);
1479
1480                                list_add_tail(SCSI_LIST(scb->cmd),
1481                                                &adapter->completed_list);
1482
1483                                mega_free_scb(adapter, scb);
1484
1485                                continue;
1486                        }
1487
1488                        /*
1489                         * Was a reset issued for this command
1490                         */
1491                        if( scb->state & SCB_RESET ) {
1492
1493                                printk(KERN_WARNING
1494                                "megaraid: reset cmd [%x] complete.\n",
1495                                        scb->idx);
1496
1497                                scb->cmd->result = (DID_RESET << 16);
1498
1499                                list_add_tail(SCSI_LIST(scb->cmd),
1500                                                &adapter->completed_list);
1501
1502                                mega_free_scb (adapter, scb);
1503
1504                                continue;
1505                        }
1506
1507                        cmd = scb->cmd;
1508                        pthru = scb->pthru;
1509                        epthru = scb->epthru;
1510                        mbox = (mbox_t *)scb->raw_mbox;
1511
1512#if MEGA_HAVE_STATS
1513                        {
1514
1515                        int     logdrv = mbox->m_out.logdrv;
1516
1517                        islogical = adapter->logdrv_chan[cmd->channel];
1518                        /*
1519                         * Maintain an error counter for the logical drive.
1520                         * Some application like SNMP agent need such
1521                         * statistics
1522                         */
1523                        if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1524                                                cmd->cmnd[0] == READ_10 ||
1525                                                cmd->cmnd[0] == READ_12)) {
1526                                /*
1527                                 * Logical drive number increases by 0x80 when
1528                                 * a logical drive is deleted
1529                                 */
1530                                adapter->rd_errors[logdrv%0x80]++;
1531                        }
1532
1533                        if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1534                                                cmd->cmnd[0] == WRITE_10 ||
1535                                                cmd->cmnd[0] == WRITE_12)) {
1536                                /*
1537                                 * Logical drive number increases by 0x80 when
1538                                 * a logical drive is deleted
1539                                 */
1540                                adapter->wr_errors[logdrv%0x80]++;
1541                        }
1542
1543                        }
1544#endif
1545                }
1546
1547                /*
1548                 * Do not return the presence of hard disk on the channel so,
1549                 * inquiry sent, and returned data==hard disk or removable
1550                 * hard disk and not logical, request should return failure! -
1551                 * PJ
1552                 */
1553                islogical = adapter->logdrv_chan[cmd->device->channel];
1554                if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1555
1556                        sgl = scsi_sglist(cmd);
1557                        if( sg_page(sgl) ) {
1558                                c = *(unsigned char *) sg_virt(&sgl[0]);
1559                        } else {
1560                                printk(KERN_WARNING
1561                                       "megaraid: invalid sg.\n");
1562                                c = 0;
1563                        }
1564
1565                        if(IS_RAID_CH(adapter, cmd->device->channel) &&
1566                                        ((c & 0x1F ) == TYPE_DISK)) {
1567                                status = 0xF0;
1568                        }
1569                }
1570
1571                /* clear result; otherwise, success returns corrupt value */
1572                cmd->result = 0;
1573
1574                /* Convert MegaRAID status to Linux error code */
1575                switch (status) {
1576                case 0x00:      /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1577                        cmd->result |= (DID_OK << 16);
1578                        break;
1579
1580                case 0x02:      /* ERROR_ABORTED, i.e.
1581                                   SCSI_STATUS_CHECK_CONDITION */
1582
1583                        /* set sense_buffer and result fields */
1584                        if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1585                                mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1586
1587                                memcpy(cmd->sense_buffer, pthru->reqsensearea,
1588                                                14);
1589
1590                                cmd->result = (DRIVER_SENSE << 24) |
1591                                        (DID_OK << 16) |
1592                                        (CHECK_CONDITION << 1);
1593                        }
1594                        else {
1595                                if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1596
1597                                        memcpy(cmd->sense_buffer,
1598                                                epthru->reqsensearea, 14);
1599
1600                                        cmd->result = (DRIVER_SENSE << 24) |
1601                                                (DID_OK << 16) |
1602                                                (CHECK_CONDITION << 1);
1603                                } else {
1604                                        cmd->sense_buffer[0] = 0x70;
1605                                        cmd->sense_buffer[2] = ABORTED_COMMAND;
1606                                        cmd->result |= (CHECK_CONDITION << 1);
1607                                }
1608                        }
1609                        break;
1610
1611                case 0x08:      /* ERR_DEST_DRIVE_FAILED, i.e.
1612                                   SCSI_STATUS_BUSY */
1613                        cmd->result |= (DID_BUS_BUSY << 16) | status;
1614                        break;
1615
1616                default:
1617#if MEGA_HAVE_CLUSTERING
1618                        /*
1619                         * If TEST_UNIT_READY fails, we know
1620                         * MEGA_RESERVATION_STATUS failed
1621                         */
1622                        if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1623                                cmd->result |= (DID_ERROR << 16) |
1624                                        (RESERVATION_CONFLICT << 1);
1625                        }
1626                        else
1627                        /*
1628                         * Error code returned is 1 if Reserve or Release
1629                         * failed or the input parameter is invalid
1630                         */
1631                        if( status == 1 &&
1632                                (cmd->cmnd[0] == RESERVE ||
1633                                         cmd->cmnd[0] == RELEASE) ) {
1634
1635                                cmd->result |= (DID_ERROR << 16) |
1636                                        (RESERVATION_CONFLICT << 1);
1637                        }
1638                        else
1639#endif
1640                                cmd->result |= (DID_BAD_TARGET << 16)|status;
1641                }
1642
1643                /*
1644                 * Only free SCBs for the commands coming down from the
1645                 * mid-layer, not for which were issued internally
1646                 *
1647                 * For internal command, restore the status returned by the
1648                 * firmware so that user can interpret it.
1649                 */
1650                if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1651                        cmd->result = status;
1652
1653                        /*
1654                         * Remove the internal command from the pending list
1655                         */
1656                        list_del_init(&scb->list);
1657                        scb->state = SCB_FREE;
1658                }
1659                else {
1660                        mega_free_scb(adapter, scb);
1661                }
1662
1663                /* Add Scsi_Command to end of completed queue */
1664                list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1665        }
1666}
1667
1668
1669/*
1670 * mega_runpendq()
1671 *
1672 * Run through the list of completed requests and finish it
1673 */
1674static void
1675mega_rundoneq (adapter_t *adapter)
1676{
1677        Scsi_Cmnd *cmd;
1678        struct list_head *pos;
1679
1680        list_for_each(pos, &adapter->completed_list) {
1681
1682                struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1683
1684                cmd = list_entry(spos, Scsi_Cmnd, SCp);
1685                cmd->scsi_done(cmd);
1686        }
1687
1688        INIT_LIST_HEAD(&adapter->completed_list);
1689}
1690
1691
1692/*
1693 * Free a SCB structure
1694 * Note: We assume the scsi commands associated with this scb is not free yet.
1695 */
1696static void
1697mega_free_scb(adapter_t *adapter, scb_t *scb)
1698{
1699        switch( scb->dma_type ) {
1700
1701        case MEGA_DMA_TYPE_NONE:
1702                break;
1703
1704        case MEGA_SGLIST:
1705                scsi_dma_unmap(scb->cmd);
1706                break;
1707        default:
1708                break;
1709        }
1710
1711        /*
1712         * Remove from the pending list
1713         */
1714        list_del_init(&scb->list);
1715
1716        /* Link the scb back into free list */
1717        scb->state = SCB_FREE;
1718        scb->cmd = NULL;
1719
1720        list_add(&scb->list, &adapter->free_list);
1721}
1722
1723
1724static int
1725__mega_busywait_mbox (adapter_t *adapter)
1726{
1727        volatile mbox_t *mbox = adapter->mbox;
1728        long counter;
1729
1730        for (counter = 0; counter < 10000; counter++) {
1731                if (!mbox->m_in.busy)
1732                        return 0;
1733                udelay(100);
1734                cond_resched();
1735        }
1736        return -1;              /* give up after 1 second */
1737}
1738
1739/*
1740 * Copies data to SGLIST
1741 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1742 */
1743static int
1744mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1745{
1746        struct scatterlist *sg;
1747        Scsi_Cmnd       *cmd;
1748        int     sgcnt;
1749        int     idx;
1750
1751        cmd = scb->cmd;
1752
1753        /*
1754         * Copy Scatter-Gather list info into controller structure.
1755         *
1756         * The number of sg elements returned must not exceed our limit
1757         */
1758        sgcnt = scsi_dma_map(cmd);
1759
1760        scb->dma_type = MEGA_SGLIST;
1761
1762        BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1763
1764        *len = 0;
1765
1766        if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1767                sg = scsi_sglist(cmd);
1768                scb->dma_h_bulkdata = sg_dma_address(sg);
1769                *buf = (u32)scb->dma_h_bulkdata;
1770                *len = sg_dma_len(sg);
1771                return 0;
1772        }
1773
1774        scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1775                if (adapter->has_64bit_addr) {
1776                        scb->sgl64[idx].address = sg_dma_address(sg);
1777                        *len += scb->sgl64[idx].length = sg_dma_len(sg);
1778                } else {
1779                        scb->sgl[idx].address = sg_dma_address(sg);
1780                        *len += scb->sgl[idx].length = sg_dma_len(sg);
1781                }
1782        }
1783
1784        /* Reset pointer and length fields */
1785        *buf = scb->sgl_dma_addr;
1786
1787        /* Return count of SG requests */
1788        return sgcnt;
1789}
1790
1791
1792/*
1793 * mega_8_to_40ld()
1794 *
1795 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1796 * Enquiry3 structures for later use
1797 */
1798static void
1799mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1800                mega_product_info *product_info)
1801{
1802        int i;
1803
1804        product_info->max_commands = inquiry->adapter_info.max_commands;
1805        enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1806        product_info->nchannels = inquiry->adapter_info.nchannels;
1807
1808        for (i = 0; i < 4; i++) {
1809                product_info->fw_version[i] =
1810                        inquiry->adapter_info.fw_version[i];
1811
1812                product_info->bios_version[i] =
1813                        inquiry->adapter_info.bios_version[i];
1814        }
1815        enquiry3->cache_flush_interval =
1816                inquiry->adapter_info.cache_flush_interval;
1817
1818        product_info->dram_size = inquiry->adapter_info.dram_size;
1819
1820        enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1821
1822        for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1823                enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1824                enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1825                enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1826        }
1827
1828        for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1829                enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1830}
1831
1832static inline void
1833mega_free_sgl(adapter_t *adapter)
1834{
1835        scb_t   *scb;
1836        int     i;
1837
1838        for(i = 0; i < adapter->max_cmds; i++) {
1839
1840                scb = &adapter->scb_list[i];
1841
1842                if( scb->sgl64 ) {
1843                        pci_free_consistent(adapter->dev,
1844                                sizeof(mega_sgl64) * adapter->sglen,
1845                                scb->sgl64,
1846                                scb->sgl_dma_addr);
1847
1848                        scb->sgl64 = NULL;
1849                }
1850
1851                if( scb->pthru ) {
1852                        pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1853                                scb->pthru, scb->pthru_dma_addr);
1854
1855                        scb->pthru = NULL;
1856                }
1857
1858                if( scb->epthru ) {
1859                        pci_free_consistent(adapter->dev,
1860                                sizeof(mega_ext_passthru),
1861                                scb->epthru, scb->epthru_dma_addr);
1862
1863                        scb->epthru = NULL;
1864                }
1865
1866        }
1867}
1868
1869
1870/*
1871 * Get information about the card/driver
1872 */
1873const char *
1874megaraid_info(struct Scsi_Host *host)
1875{
1876        static char buffer[512];
1877        adapter_t *adapter;
1878
1879        adapter = (adapter_t *)host->hostdata;
1880
1881        sprintf (buffer,
1882                 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1883                 adapter->fw_version, adapter->product_info.max_commands,
1884                 adapter->host->max_id, adapter->host->max_channel,
1885                 adapter->host->max_lun);
1886        return buffer;
1887}
1888
1889/*
1890 * Abort a previous SCSI request. Only commands on the pending list can be
1891 * aborted. All the commands issued to the F/W must complete.
1892 */
1893static int
1894megaraid_abort(Scsi_Cmnd *cmd)
1895{
1896        adapter_t       *adapter;
1897        int             rval;
1898
1899        adapter = (adapter_t *)cmd->device->host->hostdata;
1900
1901        rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1902
1903        /*
1904         * This is required here to complete any completed requests
1905         * to be communicated over to the mid layer.
1906         */
1907        mega_rundoneq(adapter);
1908
1909        return rval;
1910}
1911
1912
1913static int
1914megaraid_reset(struct scsi_cmnd *cmd)
1915{
1916        adapter_t       *adapter;
1917        megacmd_t       mc;
1918        int             rval;
1919
1920        adapter = (adapter_t *)cmd->device->host->hostdata;
1921
1922#if MEGA_HAVE_CLUSTERING
1923        mc.cmd = MEGA_CLUSTER_CMD;
1924        mc.opcode = MEGA_RESET_RESERVATIONS;
1925
1926        if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1927                printk(KERN_WARNING
1928                                "megaraid: reservation reset failed.\n");
1929        }
1930        else {
1931                printk(KERN_INFO "megaraid: reservation reset.\n");
1932        }
1933#endif
1934
1935        spin_lock_irq(&adapter->lock);
1936
1937        rval =  megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1938
1939        /*
1940         * This is required here to complete any completed requests
1941         * to be communicated over to the mid layer.
1942         */
1943        mega_rundoneq(adapter);
1944        spin_unlock_irq(&adapter->lock);
1945
1946        return rval;
1947}
1948
1949/**
1950 * megaraid_abort_and_reset()
1951 * @adapter - megaraid soft state
1952 * @cmd - scsi command to be aborted or reset
1953 * @aor - abort or reset flag
1954 *
1955 * Try to locate the scsi command in the pending queue. If found and is not
1956 * issued to the controller, abort/reset it. Otherwise return failure
1957 */
1958static int
1959megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1960{
1961        struct list_head        *pos, *next;
1962        scb_t                   *scb;
1963
1964        printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>\n",
1965             (aor == SCB_ABORT)? "ABORTING":"RESET",
1966             cmd->cmnd[0], cmd->device->channel, 
1967             cmd->device->id, cmd->device->lun);
1968
1969        if(list_empty(&adapter->pending_list))
1970                return FALSE;
1971
1972        list_for_each_safe(pos, next, &adapter->pending_list) {
1973
1974                scb = list_entry(pos, scb_t, list);
1975
1976                if (scb->cmd == cmd) { /* Found command */
1977
1978                        scb->state |= aor;
1979
1980                        /*
1981                         * Check if this command has firmware ownership. If
1982                         * yes, we cannot reset this command. Whenever f/w
1983                         * completes this command, we will return appropriate
1984                         * status from ISR.
1985                         */
1986                        if( scb->state & SCB_ISSUED ) {
1987
1988                                printk(KERN_WARNING
1989                                        "megaraid: %s[%x], fw owner.\n",
1990                                        (aor==SCB_ABORT) ? "ABORTING":"RESET",
1991                                        scb->idx);
1992
1993                                return FALSE;
1994                        }
1995                        else {
1996
1997                                /*
1998                                 * Not yet issued! Remove from the pending
1999                                 * list
2000                                 */
2001                                printk(KERN_WARNING
2002                                        "megaraid: %s-[%x], driver owner.\n",
2003                                        (aor==SCB_ABORT) ? "ABORTING":"RESET",
2004                                        scb->idx);
2005
2006                                mega_free_scb(adapter, scb);
2007
2008                                if( aor == SCB_ABORT ) {
2009                                        cmd->result = (DID_ABORT << 16);
2010                                }
2011                                else {
2012                                        cmd->result = (DID_RESET << 16);
2013                                }
2014
2015                                list_add_tail(SCSI_LIST(cmd),
2016                                                &adapter->completed_list);
2017
2018                                return TRUE;
2019                        }
2020                }
2021        }
2022
2023        return FALSE;
2024}
2025
2026static inline int
2027make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2028{
2029        *pdev = alloc_pci_dev();
2030
2031        if( *pdev == NULL ) return -1;
2032
2033        memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2034
2035        if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2036                kfree(*pdev);
2037                return -1;
2038        }
2039
2040        return 0;
2041}
2042
2043static inline void
2044free_local_pdev(struct pci_dev *pdev)
2045{
2046        kfree(pdev);
2047}
2048
2049/**
2050 * mega_allocate_inquiry()
2051 * @dma_handle - handle returned for dma address
2052 * @pdev - handle to pci device
2053 *
2054 * allocates memory for inquiry structure
2055 */
2056static inline void *
2057mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2058{
2059        return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2060}
2061
2062
2063static inline void
2064mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2065{
2066        pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2067}
2068
2069
2070#ifdef CONFIG_PROC_FS
2071/* Following code handles /proc fs  */
2072
2073/**
2074 * proc_show_config()
2075 * @m - Synthetic file construction data
2076 * @v - File iterator
2077 *
2078 * Display configuration information about the controller.
2079 */
2080static int
2081proc_show_config(struct seq_file *m, void *v)
2082{
2083
2084        adapter_t *adapter = m->private;
2085
2086        seq_puts(m, MEGARAID_VERSION);
2087        if(adapter->product_info.product_name[0])
2088                seq_printf(m, "%s\n", adapter->product_info.product_name);
2089
2090        seq_puts(m, "Controller Type: ");
2091
2092        if( adapter->flag & BOARD_MEMMAP )
2093                seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2094        else
2095                seq_puts(m, "418/428/434\n");
2096
2097        if(adapter->flag & BOARD_40LD)
2098                seq_puts(m, "Controller Supports 40 Logical Drives\n");
2099
2100        if(adapter->flag & BOARD_64BIT)
2101                seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2102        if( adapter->has_64bit_addr )
2103                seq_puts(m, "Controller using 64-bit memory addressing\n");
2104        else
2105                seq_puts(m, "Controller is not using 64-bit memory addressing\n");
2106
2107        seq_printf(m, "Base = %08lx, Irq = %d, ",
2108                   adapter->base, adapter->host->irq);
2109
2110        seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2111                   adapter->numldrv, adapter->product_info.nchannels);
2112
2113        seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2114                   adapter->fw_version, adapter->bios_version,
2115                   adapter->product_info.dram_size);
2116
2117        seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2118                   adapter->product_info.max_commands, adapter->max_cmds);
2119
2120        seq_printf(m, "support_ext_cdb    = %d\n", adapter->support_ext_cdb);
2121        seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2122        seq_printf(m, "boot_ldrv_enabled  = %d\n", adapter->boot_ldrv_enabled);
2123        seq_printf(m, "boot_ldrv          = %d\n", adapter->boot_ldrv);
2124        seq_printf(m, "boot_pdrv_enabled  = %d\n", adapter->boot_pdrv_enabled);
2125        seq_printf(m, "boot_pdrv_ch       = %d\n", adapter->boot_pdrv_ch);
2126        seq_printf(m, "boot_pdrv_tgt      = %d\n", adapter->boot_pdrv_tgt);
2127        seq_printf(m, "quiescent          = %d\n",
2128                   atomic_read(&adapter->quiescent));
2129        seq_printf(m, "has_cluster        = %d\n", adapter->has_cluster);
2130
2131        seq_puts(m, "\nModule Parameters:\n");
2132        seq_printf(m, "max_cmd_per_lun    = %d\n", max_cmd_per_lun);
2133        seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2134        return 0;
2135}
2136
2137/**
2138 * proc_show_stat()
2139 * @m - Synthetic file construction data
2140 * @v - File iterator
2141 *
2142 * Display statistical information about the I/O activity.
2143 */
2144static int
2145proc_show_stat(struct seq_file *m, void *v)
2146{
2147        adapter_t *adapter = m->private;
2148#if MEGA_HAVE_STATS
2149        int     i;
2150#endif
2151
2152        seq_puts(m, "Statistical Information for this controller\n");
2153        seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2154#if MEGA_HAVE_STATS
2155        for(i = 0; i < adapter->numldrv; i++) {
2156                seq_printf(m, "Logical Drive %d:\n", i);
2157                seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2158                           adapter->nreads[i], adapter->nwrites[i]);
2159                seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2160                           adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2161                seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2162                           adapter->rd_errors[i], adapter->wr_errors[i]);
2163        }
2164#else
2165        seq_puts(m, "IO and error counters not compiled in driver.\n");
2166#endif
2167        return 0;
2168}
2169
2170
2171/**
2172 * proc_show_mbox()
2173 * @m - Synthetic file construction data
2174 * @v - File iterator
2175 *
2176 * Display mailbox information for the last command issued. This information
2177 * is good for debugging.
2178 */
2179static int
2180proc_show_mbox(struct seq_file *m, void *v)
2181{
2182        adapter_t       *adapter = m->private;
2183        volatile mbox_t *mbox = adapter->mbox;
2184
2185        seq_puts(m, "Contents of Mail Box Structure\n");
2186        seq_printf(m, "  Fw Command   = 0x%02x\n", mbox->m_out.cmd);
2187        seq_printf(m, "  Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2188        seq_printf(m, "  No of Sectors= %04d\n", mbox->m_out.numsectors);
2189        seq_printf(m, "  LBA          = 0x%02x\n", mbox->m_out.lba);
2190        seq_printf(m, "  DTA          = 0x%08x\n", mbox->m_out.xferaddr);
2191        seq_printf(m, "  Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2192        seq_printf(m, "  No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2193        seq_printf(m, "  Busy         = %01x\n", mbox->m_in.busy);
2194        seq_printf(m, "  Status       = 0x%02x\n", mbox->m_in.status);
2195        return 0;
2196}
2197
2198
2199/**
2200 * proc_show_rebuild_rate()
2201 * @m - Synthetic file construction data
2202 * @v - File iterator
2203 *
2204 * Display current rebuild rate
2205 */
2206static int
2207proc_show_rebuild_rate(struct seq_file *m, void *v)
2208{
2209        adapter_t       *adapter = m->private;
2210        dma_addr_t      dma_handle;
2211        caddr_t         inquiry;
2212        struct pci_dev  *pdev;
2213
2214        if( make_local_pdev(adapter, &pdev) != 0 )
2215                return 0;
2216
2217        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2218                goto free_pdev;
2219
2220        if( mega_adapinq(adapter, dma_handle) != 0 ) {
2221                seq_puts(m, "Adapter inquiry failed.\n");
2222                printk(KERN_WARNING "megaraid: inquiry failed.\n");
2223                goto free_inquiry;
2224        }
2225
2226        if( adapter->flag & BOARD_40LD )
2227                seq_printf(m, "Rebuild Rate: [%d%%]\n",
2228                           ((mega_inquiry3 *)inquiry)->rebuild_rate);
2229        else
2230                seq_printf(m, "Rebuild Rate: [%d%%]\n",
2231                        ((mraid_ext_inquiry *)
2232                         inquiry)->raid_inq.adapter_info.rebuild_rate);
2233
2234free_inquiry:
2235        mega_free_inquiry(inquiry, dma_handle, pdev);
2236free_pdev:
2237        free_local_pdev(pdev);
2238        return 0;
2239}
2240
2241
2242/**
2243 * proc_show_battery()
2244 * @m - Synthetic file construction data
2245 * @v - File iterator
2246 *
2247 * Display information about the battery module on the controller.
2248 */
2249static int
2250proc_show_battery(struct seq_file *m, void *v)
2251{
2252        adapter_t       *adapter = m->private;
2253        dma_addr_t      dma_handle;
2254        caddr_t         inquiry;
2255        struct pci_dev  *pdev;
2256        u8      battery_status;
2257
2258        if( make_local_pdev(adapter, &pdev) != 0 )
2259                return 0;
2260
2261        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2262                goto free_pdev;
2263
2264        if( mega_adapinq(adapter, dma_handle) != 0 ) {
2265                seq_printf(m, "Adapter inquiry failed.\n");
2266                printk(KERN_WARNING "megaraid: inquiry failed.\n");
2267                goto free_inquiry;
2268        }
2269
2270        if( adapter->flag & BOARD_40LD ) {
2271                battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2272        }
2273        else {
2274                battery_status = ((mraid_ext_inquiry *)inquiry)->
2275                        raid_inq.adapter_info.battery_status;
2276        }
2277
2278        /*
2279         * Decode the battery status
2280         */
2281        seq_printf(m, "Battery Status:[%d]", battery_status);
2282
2283        if(battery_status == MEGA_BATT_CHARGE_DONE)
2284                seq_puts(m, " Charge Done");
2285
2286        if(battery_status & MEGA_BATT_MODULE_MISSING)
2287                seq_puts(m, " Module Missing");
2288        
2289        if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2290                seq_puts(m, " Low Voltage");
2291        
2292        if(battery_status & MEGA_BATT_TEMP_HIGH)
2293                seq_puts(m, " Temperature High");
2294        
2295        if(battery_status & MEGA_BATT_PACK_MISSING)
2296                seq_puts(m, " Pack Missing");
2297        
2298        if(battery_status & MEGA_BATT_CHARGE_INPROG)
2299                seq_puts(m, " Charge In-progress");
2300        
2301        if(battery_status & MEGA_BATT_CHARGE_FAIL)
2302                seq_puts(m, " Charge Fail");
2303        
2304        if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2305                seq_puts(m, " Cycles Exceeded");
2306
2307        seq_putc(m, '\n');
2308
2309free_inquiry:
2310        mega_free_inquiry(inquiry, dma_handle, pdev);
2311free_pdev:
2312        free_local_pdev(pdev);
2313        return 0;
2314}
2315
2316
2317/*
2318 * Display scsi inquiry
2319 */
2320static void
2321mega_print_inquiry(struct seq_file *m, char *scsi_inq)
2322{
2323        int     i;
2324
2325        seq_puts(m, "  Vendor: ");
2326        seq_write(m, scsi_inq + 8, 8);
2327        seq_puts(m, "  Model: ");
2328        seq_write(m, scsi_inq + 16, 16);
2329        seq_puts(m, "  Rev: ");
2330        seq_write(m, scsi_inq + 32, 4);
2331        seq_putc(m, '\n');
2332
2333        i = scsi_inq[0] & 0x1f;
2334        seq_printf(m, "  Type:   %s ", scsi_device_type(i));
2335
2336        seq_printf(m, "                 ANSI SCSI revision: %02x",
2337                   scsi_inq[2] & 0x07);
2338
2339        if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2340                seq_puts(m, " CCS\n");
2341        else
2342                seq_putc(m, '\n');
2343}
2344
2345/**
2346 * proc_show_pdrv()
2347 * @m - Synthetic file construction data
2348 * @page - buffer to write the data in
2349 * @adapter - pointer to our soft state
2350 *
2351 * Display information about the physical drives.
2352 */
2353static int
2354proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
2355{
2356        dma_addr_t      dma_handle;
2357        char            *scsi_inq;
2358        dma_addr_t      scsi_inq_dma_handle;
2359        caddr_t         inquiry;
2360        struct pci_dev  *pdev;
2361        u8      *pdrv_state;
2362        u8      state;
2363        int     tgt;
2364        int     max_channels;
2365        int     i;
2366
2367        if( make_local_pdev(adapter, &pdev) != 0 )
2368                return 0;
2369
2370        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2371                goto free_pdev;
2372
2373        if( mega_adapinq(adapter, dma_handle) != 0 ) {
2374                seq_puts(m, "Adapter inquiry failed.\n");
2375                printk(KERN_WARNING "megaraid: inquiry failed.\n");
2376                goto free_inquiry;
2377        }
2378
2379
2380        scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2381        if( scsi_inq == NULL ) {
2382                seq_puts(m, "memory not available for scsi inq.\n");
2383                goto free_inquiry;
2384        }
2385
2386        if( adapter->flag & BOARD_40LD ) {
2387                pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2388        }
2389        else {
2390                pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2391                        raid_inq.pdrv_info.pdrv_state;
2392        }
2393
2394        max_channels = adapter->product_info.nchannels;
2395
2396        if( channel >= max_channels ) {
2397                goto free_pci;
2398        }
2399
2400        for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2401
2402                i = channel*16 + tgt;
2403
2404                state = *(pdrv_state + i);
2405                switch( state & 0x0F ) {
2406                case PDRV_ONLINE:
2407                        seq_printf(m, "Channel:%2d Id:%2d State: Online",
2408                                   channel, tgt);
2409                        break;
2410
2411                case PDRV_FAILED:
2412                        seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2413                                   channel, tgt);
2414                        break;
2415
2416                case PDRV_RBLD:
2417                        seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2418                                   channel, tgt);
2419                        break;
2420
2421                case PDRV_HOTSPARE:
2422                        seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2423                                   channel, tgt);
2424                        break;
2425
2426                default:
2427                        seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2428                                   channel, tgt);
2429                        break;
2430                }
2431
2432                /*
2433                 * This interface displays inquiries for disk drives
2434                 * only. Inquries for logical drives and non-disk
2435                 * devices are available through /proc/scsi/scsi
2436                 */
2437                memset(scsi_inq, 0, 256);
2438                if( mega_internal_dev_inquiry(adapter, channel, tgt,
2439                                scsi_inq_dma_handle) ||
2440                                (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2441                        continue;
2442                }
2443
2444                /*
2445                 * Check for overflow. We print less than 240
2446                 * characters for inquiry
2447                 */
2448                seq_puts(m, ".\n");
2449                mega_print_inquiry(m, scsi_inq);
2450        }
2451
2452free_pci:
2453        pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2454free_inquiry:
2455        mega_free_inquiry(inquiry, dma_handle, pdev);
2456free_pdev:
2457        free_local_pdev(pdev);
2458        return 0;
2459}
2460
2461/**
2462 * proc_show_pdrv_ch0()
2463 * @m - Synthetic file construction data
2464 * @v - File iterator
2465 *
2466 * Display information about the physical drives on physical channel 0.
2467 */
2468static int
2469proc_show_pdrv_ch0(struct seq_file *m, void *v)
2470{
2471        return proc_show_pdrv(m, m->private, 0);
2472}
2473
2474
2475/**
2476 * proc_show_pdrv_ch1()
2477 * @m - Synthetic file construction data
2478 * @v - File iterator
2479 *
2480 * Display information about the physical drives on physical channel 1.
2481 */
2482static int
2483proc_show_pdrv_ch1(struct seq_file *m, void *v)
2484{
2485        return proc_show_pdrv(m, m->private, 1);
2486}
2487
2488
2489/**
2490 * proc_show_pdrv_ch2()
2491 * @m - Synthetic file construction data
2492 * @v - File iterator
2493 *
2494 * Display information about the physical drives on physical channel 2.
2495 */
2496static int
2497proc_show_pdrv_ch2(struct seq_file *m, void *v)
2498{
2499        return proc_show_pdrv(m, m->private, 2);
2500}
2501
2502
2503/**
2504 * proc_show_pdrv_ch3()
2505 * @m - Synthetic file construction data
2506 * @v - File iterator
2507 *
2508 * Display information about the physical drives on physical channel 3.
2509 */
2510static int
2511proc_show_pdrv_ch3(struct seq_file *m, void *v)
2512{
2513        return proc_show_pdrv(m, m->private, 3);
2514}
2515
2516
2517/**
2518 * proc_show_rdrv()
2519 * @m - Synthetic file construction data
2520 * @adapter - pointer to our soft state
2521 * @start - starting logical drive to display
2522 * @end - ending logical drive to display
2523 *
2524 * We do not print the inquiry information since its already available through
2525 * /proc/scsi/scsi interface
2526 */
2527static int
2528proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
2529{
2530        dma_addr_t      dma_handle;
2531        logdrv_param    *lparam;
2532        megacmd_t       mc;
2533        char            *disk_array;
2534        dma_addr_t      disk_array_dma_handle;
2535        caddr_t         inquiry;
2536        struct pci_dev  *pdev;
2537        u8      *rdrv_state;
2538        int     num_ldrv;
2539        u32     array_sz;
2540        int     i;
2541
2542        if( make_local_pdev(adapter, &pdev) != 0 )
2543                return 0;
2544
2545        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2546                goto free_pdev;
2547
2548        if( mega_adapinq(adapter, dma_handle) != 0 ) {
2549                seq_puts(m, "Adapter inquiry failed.\n");
2550                printk(KERN_WARNING "megaraid: inquiry failed.\n");
2551                goto free_inquiry;
2552        }
2553
2554        memset(&mc, 0, sizeof(megacmd_t));
2555
2556        if( adapter->flag & BOARD_40LD ) {
2557                array_sz = sizeof(disk_array_40ld);
2558
2559                rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2560
2561                num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2562        }
2563        else {
2564                array_sz = sizeof(disk_array_8ld);
2565
2566                rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2567                        raid_inq.logdrv_info.ldrv_state;
2568
2569                num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2570                        raid_inq.logdrv_info.num_ldrv;
2571        }
2572
2573        disk_array = pci_alloc_consistent(pdev, array_sz,
2574                        &disk_array_dma_handle);
2575
2576        if( disk_array == NULL ) {
2577                seq_puts(m, "memory not available.\n");
2578                goto free_inquiry;
2579        }
2580
2581        mc.xferaddr = (u32)disk_array_dma_handle;
2582
2583        if( adapter->flag & BOARD_40LD ) {
2584                mc.cmd = FC_NEW_CONFIG;
2585                mc.opcode = OP_DCMD_READ_CONFIG;
2586
2587                if( mega_internal_command(adapter, &mc, NULL) ) {
2588                        seq_puts(m, "40LD read config failed.\n");
2589                        goto free_pci;
2590                }
2591
2592        }
2593        else {
2594                mc.cmd = NEW_READ_CONFIG_8LD;
2595
2596                if( mega_internal_command(adapter, &mc, NULL) ) {
2597                        mc.cmd = READ_CONFIG_8LD;
2598                        if( mega_internal_command(adapter, &mc, NULL) ) {
2599                                seq_puts(m, "8LD read config failed.\n");
2600                                goto free_pci;
2601                        }
2602                }
2603        }
2604
2605        for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2606
2607                if( adapter->flag & BOARD_40LD ) {
2608                        lparam =
2609                        &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2610                }
2611                else {
2612                        lparam =
2613                        &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2614                }
2615
2616                /*
2617                 * Check for overflow. We print less than 240 characters for
2618                 * information about each logical drive.
2619                 */
2620                seq_printf(m, "Logical drive:%2d:, ", i);
2621
2622                switch( rdrv_state[i] & 0x0F ) {
2623                case RDRV_OFFLINE:
2624                        seq_puts(m, "state: offline");
2625                        break;
2626                case RDRV_DEGRADED:
2627                        seq_puts(m, "state: degraded");
2628                        break;
2629                case RDRV_OPTIMAL:
2630                        seq_puts(m, "state: optimal");
2631                        break;
2632                case RDRV_DELETED:
2633                        seq_puts(m, "state: deleted");
2634                        break;
2635                default:
2636                        seq_puts(m, "state: unknown");
2637                        break;
2638                }
2639
2640                /*
2641                 * Check if check consistency or initialization is going on
2642                 * for this logical drive.
2643                 */
2644                if( (rdrv_state[i] & 0xF0) == 0x20 )
2645                        seq_puts(m, ", check-consistency in progress");
2646                else if( (rdrv_state[i] & 0xF0) == 0x10 )
2647                        seq_puts(m, ", initialization in progress");
2648                
2649                seq_putc(m, '\n');
2650
2651                seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2652                seq_printf(m, "RAID level:%3d, ", lparam->level);
2653                seq_printf(m, "Stripe size:%3d, ",
2654                           lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2655                seq_printf(m, "Row size:%3d\n", lparam->row_size);
2656
2657                seq_puts(m, "Read Policy: ");
2658                switch(lparam->read_ahead) {
2659                case NO_READ_AHEAD:
2660                        seq_puts(m, "No read ahead, ");
2661                        break;
2662                case READ_AHEAD:
2663                        seq_puts(m, "Read ahead, ");
2664                        break;
2665                case ADAP_READ_AHEAD:
2666                        seq_puts(m, "Adaptive, ");
2667                        break;
2668
2669                }
2670
2671                seq_puts(m, "Write Policy: ");
2672                switch(lparam->write_mode) {
2673                case WRMODE_WRITE_THRU:
2674                        seq_puts(m, "Write thru, ");
2675                        break;
2676                case WRMODE_WRITE_BACK:
2677                        seq_puts(m, "Write back, ");
2678                        break;
2679                }
2680
2681                seq_puts(m, "Cache Policy: ");
2682                switch(lparam->direct_io) {
2683                case CACHED_IO:
2684                        seq_puts(m, "Cached IO\n\n");
2685                        break;
2686                case DIRECT_IO:
2687                        seq_puts(m, "Direct IO\n\n");
2688                        break;
2689                }
2690        }
2691
2692free_pci:
2693        pci_free_consistent(pdev, array_sz, disk_array,
2694                        disk_array_dma_handle);
2695free_inquiry:
2696        mega_free_inquiry(inquiry, dma_handle, pdev);
2697free_pdev:
2698        free_local_pdev(pdev);
2699        return 0;
2700}
2701
2702/**
2703 * proc_show_rdrv_10()
2704 * @m - Synthetic file construction data
2705 * @v - File iterator
2706 *
2707 * Display real time information about the logical drives 0 through 9.
2708 */
2709static int
2710proc_show_rdrv_10(struct seq_file *m, void *v)
2711{
2712        return proc_show_rdrv(m, m->private, 0, 9);
2713}
2714
2715
2716/**
2717 * proc_show_rdrv_20()
2718 * @m - Synthetic file construction data
2719 * @v - File iterator
2720 *
2721 * Display real time information about the logical drives 0 through 9.
2722 */
2723static int
2724proc_show_rdrv_20(struct seq_file *m, void *v)
2725{
2726        return proc_show_rdrv(m, m->private, 10, 19);
2727}
2728
2729
2730/**
2731 * proc_show_rdrv_30()
2732 * @m - Synthetic file construction data
2733 * @v - File iterator
2734 *
2735 * Display real time information about the logical drives 0 through 9.
2736 */
2737static int
2738proc_show_rdrv_30(struct seq_file *m, void *v)
2739{
2740        return proc_show_rdrv(m, m->private, 20, 29);
2741}
2742
2743
2744/**
2745 * proc_show_rdrv_40()
2746 * @m - Synthetic file construction data
2747 * @v - File iterator
2748 *
2749 * Display real time information about the logical drives 0 through 9.
2750 */
2751static int
2752proc_show_rdrv_40(struct seq_file *m, void *v)
2753{
2754        return proc_show_rdrv(m, m->private, 30, 39);
2755}
2756
2757
2758/*
2759 * seq_file wrappers for procfile show routines.
2760 */
2761static int mega_proc_open(struct inode *inode, struct file *file)
2762{
2763        adapter_t *adapter = proc_get_parent_data(inode);
2764        int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
2765
2766        return single_open(file, show, adapter);
2767}
2768
2769static const struct file_operations mega_proc_fops = {
2770        .open           = mega_proc_open,
2771        .read           = seq_read,
2772        .llseek         = seq_lseek,
2773        .release        = single_release,
2774};
2775
2776/*
2777 * Table of proc files we need to create.
2778 */
2779struct mega_proc_file {
2780        const char *name;
2781        unsigned short ptr_offset;
2782        int (*show) (struct seq_file *m, void *v);
2783};
2784
2785static const struct mega_proc_file mega_proc_files[] = {
2786        { "config",           offsetof(adapter_t, proc_read), proc_show_config },
2787        { "stat",             offsetof(adapter_t, proc_stat), proc_show_stat },
2788        { "mailbox",          offsetof(adapter_t, proc_mbox), proc_show_mbox },
2789#if MEGA_HAVE_ENH_PROC
2790        { "rebuild-rate",     offsetof(adapter_t, proc_rr), proc_show_rebuild_rate },
2791        { "battery-status",   offsetof(adapter_t, proc_battery), proc_show_battery },
2792        { "diskdrives-ch0",   offsetof(adapter_t, proc_pdrvstat[0]), proc_show_pdrv_ch0 },
2793        { "diskdrives-ch1",   offsetof(adapter_t, proc_pdrvstat[1]), proc_show_pdrv_ch1 },
2794        { "diskdrives-ch2",   offsetof(adapter_t, proc_pdrvstat[2]), proc_show_pdrv_ch2 },
2795        { "diskdrives-ch3",   offsetof(adapter_t, proc_pdrvstat[3]), proc_show_pdrv_ch3 },
2796        { "raiddrives-0-9",   offsetof(adapter_t, proc_rdrvstat[0]), proc_show_rdrv_10 },
2797        { "raiddrives-10-19", offsetof(adapter_t, proc_rdrvstat[1]), proc_show_rdrv_20 },
2798        { "raiddrives-20-29", offsetof(adapter_t, proc_rdrvstat[2]), proc_show_rdrv_30 },
2799        { "raiddrives-30-39", offsetof(adapter_t, proc_rdrvstat[3]), proc_show_rdrv_40 },
2800#endif
2801        { NULL }
2802};
2803
2804/**
2805 * mega_create_proc_entry()
2806 * @index - index in soft state array
2807 * @parent - parent node for this /proc entry
2808 *
2809 * Creates /proc entries for our controllers.
2810 */
2811static void
2812mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2813{
2814        const struct mega_proc_file *f;
2815        adapter_t       *adapter = hba_soft_state[index];
2816        struct proc_dir_entry   *dir, *de, **ppde;
2817        u8              string[16];
2818
2819        sprintf(string, "hba%d", adapter->host->host_no);
2820
2821        dir = adapter->controller_proc_dir_entry =
2822                proc_mkdir_data(string, 0, parent, adapter);
2823        if(!dir) {
2824                printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2825                return;
2826        }
2827
2828        for (f = mega_proc_files; f->name; f++) {
2829                de = proc_create_data(f->name, S_IRUSR, dir, &mega_proc_fops,
2830                                      f->show);
2831                if (!de) {
2832                        printk(KERN_WARNING "\nmegaraid: proc_create failed\n");
2833                        return;
2834                }
2835
2836                ppde = (void *)adapter + f->ptr_offset;
2837                *ppde = de;
2838        }
2839}
2840
2841#else
2842static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2843{
2844}
2845#endif
2846
2847
2848/**
2849 * megaraid_biosparam()
2850 *
2851 * Return the disk geometry for a particular disk
2852 */
2853static int
2854megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2855                    sector_t capacity, int geom[])
2856{
2857        adapter_t       *adapter;
2858        unsigned char   *bh;
2859        int     heads;
2860        int     sectors;
2861        int     cylinders;
2862        int     rval;
2863
2864        /* Get pointer to host config structure */
2865        adapter = (adapter_t *)sdev->host->hostdata;
2866
2867        if (IS_RAID_CH(adapter, sdev->channel)) {
2868                        /* Default heads (64) & sectors (32) */
2869                        heads = 64;
2870                        sectors = 32;
2871                        cylinders = (ulong)capacity / (heads * sectors);
2872
2873                        /*
2874                         * Handle extended translation size for logical drives
2875                         * > 1Gb
2876                         */
2877                        if ((ulong)capacity >= 0x200000) {
2878                                heads = 255;
2879                                sectors = 63;
2880                                cylinders = (ulong)capacity / (heads * sectors);
2881                        }
2882
2883                        /* return result */
2884                        geom[0] = heads;
2885                        geom[1] = sectors;
2886                        geom[2] = cylinders;
2887        }
2888        else {
2889                bh = scsi_bios_ptable(bdev);
2890
2891                if( bh ) {
2892                        rval = scsi_partsize(bh, capacity,
2893                                            &geom[2], &geom[0], &geom[1]);
2894                        kfree(bh);
2895                        if( rval != -1 )
2896                                return rval;
2897                }
2898
2899                printk(KERN_INFO
2900                "megaraid: invalid partition on this disk on channel %d\n",
2901                                sdev->channel);
2902
2903                /* Default heads (64) & sectors (32) */
2904                heads = 64;
2905                sectors = 32;
2906                cylinders = (ulong)capacity / (heads * sectors);
2907
2908                /* Handle extended translation size for logical drives > 1Gb */
2909                if ((ulong)capacity >= 0x200000) {
2910                        heads = 255;
2911                        sectors = 63;
2912                        cylinders = (ulong)capacity / (heads * sectors);
2913                }
2914
2915                /* return result */
2916                geom[0] = heads;
2917                geom[1] = sectors;
2918                geom[2] = cylinders;
2919        }
2920
2921        return 0;
2922}
2923
2924/**
2925 * mega_init_scb()
2926 * @adapter - pointer to our soft state
2927 *
2928 * Allocate memory for the various pointers in the scb structures:
2929 * scatter-gather list pointer, passthru and extended passthru structure
2930 * pointers.
2931 */
2932static int
2933mega_init_scb(adapter_t *adapter)
2934{
2935        scb_t   *scb;
2936        int     i;
2937
2938        for( i = 0; i < adapter->max_cmds; i++ ) {
2939
2940                scb = &adapter->scb_list[i];
2941
2942                scb->sgl64 = NULL;
2943                scb->sgl = NULL;
2944                scb->pthru = NULL;
2945                scb->epthru = NULL;
2946        }
2947
2948        for( i = 0; i < adapter->max_cmds; i++ ) {
2949
2950                scb = &adapter->scb_list[i];
2951
2952                scb->idx = i;
2953
2954                scb->sgl64 = pci_alloc_consistent(adapter->dev,
2955                                sizeof(mega_sgl64) * adapter->sglen,
2956                                &scb->sgl_dma_addr);
2957
2958                scb->sgl = (mega_sglist *)scb->sgl64;
2959
2960                if( !scb->sgl ) {
2961                        printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
2962                        mega_free_sgl(adapter);
2963                        return -1;
2964                }
2965
2966                scb->pthru = pci_alloc_consistent(adapter->dev,
2967                                sizeof(mega_passthru),
2968                                &scb->pthru_dma_addr);
2969
2970                if( !scb->pthru ) {
2971                        printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
2972                        mega_free_sgl(adapter);
2973                        return -1;
2974                }
2975
2976                scb->epthru = pci_alloc_consistent(adapter->dev,
2977                                sizeof(mega_ext_passthru),
2978                                &scb->epthru_dma_addr);
2979
2980                if( !scb->epthru ) {
2981                        printk(KERN_WARNING
2982                                "Can't allocate extended passthru.\n");
2983                        mega_free_sgl(adapter);
2984                        return -1;
2985                }
2986
2987
2988                scb->dma_type = MEGA_DMA_TYPE_NONE;
2989
2990                /*
2991                 * Link to free list
2992                 * lock not required since we are loading the driver, so no
2993                 * commands possible right now.
2994                 */
2995                scb->state = SCB_FREE;
2996                scb->cmd = NULL;
2997                list_add(&scb->list, &adapter->free_list);
2998        }
2999
3000        return 0;
3001}
3002
3003
3004/**
3005 * megadev_open()
3006 * @inode - unused
3007 * @filep - unused
3008 *
3009 * Routines for the character/ioctl interface to the driver. Find out if this
3010 * is a valid open. 
3011 */
3012static int
3013megadev_open (struct inode *inode, struct file *filep)
3014{
3015        /*
3016         * Only allow superuser to access private ioctl interface
3017         */
3018        if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3019
3020        return 0;
3021}
3022
3023
3024/**
3025 * megadev_ioctl()
3026 * @inode - Our device inode
3027 * @filep - unused
3028 * @cmd - ioctl command
3029 * @arg - user buffer
3030 *
3031 * ioctl entry point for our private ioctl interface. We move the data in from
3032 * the user space, prepare the command (if necessary, convert the old MIMD
3033 * ioctl to new ioctl command), and issue a synchronous command to the
3034 * controller.
3035 */
3036static int
3037megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3038{
3039        adapter_t       *adapter;
3040        nitioctl_t      uioc;
3041        int             adapno;
3042        int             rval;
3043        mega_passthru   __user *upthru; /* user address for passthru */
3044        mega_passthru   *pthru;         /* copy user passthru here */
3045        dma_addr_t      pthru_dma_hndl;
3046        void            *data = NULL;   /* data to be transferred */
3047        dma_addr_t      data_dma_hndl;  /* dma handle for data xfer area */
3048        megacmd_t       mc;
3049        megastat_t      __user *ustats;
3050        int             num_ldrv;
3051        u32             uxferaddr = 0;
3052        struct pci_dev  *pdev;
3053
3054        ustats = NULL; /* avoid compilation warnings */
3055        num_ldrv = 0;
3056
3057        /*
3058         * Make sure only USCSICMD are issued through this interface.
3059         * MIMD application would still fire different command.
3060         */
3061        if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3062                return -EINVAL;
3063        }
3064
3065        /*
3066         * Check and convert a possible MIMD command to NIT command.
3067         * mega_m_to_n() copies the data from the user space, so we do not
3068         * have to do it here.
3069         * NOTE: We will need some user address to copyout the data, therefore
3070         * the inteface layer will also provide us with the required user
3071         * addresses.
3072         */
3073        memset(&uioc, 0, sizeof(nitioctl_t));
3074        if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3075                return rval;
3076
3077
3078        switch( uioc.opcode ) {
3079
3080        case GET_DRIVER_VER:
3081                if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3082                        return (-EFAULT);
3083
3084                break;
3085
3086        case GET_N_ADAP:
3087                if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3088                        return (-EFAULT);
3089
3090                /*
3091                 * Shucks. MIMD interface returns a positive value for number
3092                 * of adapters. TODO: Change it to return 0 when there is no
3093                 * applicatio using mimd interface.
3094                 */
3095                return hba_count;
3096
3097        case GET_ADAP_INFO:
3098
3099                /*
3100                 * Which adapter
3101                 */
3102                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3103                        return (-ENODEV);
3104
3105                if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3106                                sizeof(struct mcontroller)) )
3107                        return (-EFAULT);
3108                break;
3109
3110#if MEGA_HAVE_STATS
3111
3112        case GET_STATS:
3113                /*
3114                 * Which adapter
3115                 */
3116                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3117                        return (-ENODEV);
3118
3119                adapter = hba_soft_state[adapno];
3120
3121                ustats = uioc.uioc_uaddr;
3122
3123                if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3124                        return (-EFAULT);
3125
3126                /*
3127                 * Check for the validity of the logical drive number
3128                 */
3129                if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3130
3131                if( copy_to_user(ustats->nreads, adapter->nreads,
3132                                        num_ldrv*sizeof(u32)) )
3133                        return -EFAULT;
3134
3135                if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3136                                        num_ldrv*sizeof(u32)) )
3137                        return -EFAULT;
3138
3139                if( copy_to_user(ustats->nwrites, adapter->nwrites,
3140                                        num_ldrv*sizeof(u32)) )
3141                        return -EFAULT;
3142
3143                if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3144                                        num_ldrv*sizeof(u32)) )
3145                        return -EFAULT;
3146
3147                if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3148                                        num_ldrv*sizeof(u32)) )
3149                        return -EFAULT;
3150
3151                if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3152                                        num_ldrv*sizeof(u32)) )
3153                        return -EFAULT;
3154
3155                return 0;
3156
3157#endif
3158        case MBOX_CMD:
3159
3160                /*
3161                 * Which adapter
3162                 */
3163                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3164                        return (-ENODEV);
3165
3166                adapter = hba_soft_state[adapno];
3167
3168                /*
3169                 * Deletion of logical drive is a special case. The adapter
3170                 * should be quiescent before this command is issued.
3171                 */
3172                if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3173                                uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3174
3175                        /*
3176                         * Do we support this feature
3177                         */
3178                        if( !adapter->support_random_del ) {
3179                                printk(KERN_WARNING "megaraid: logdrv ");
3180                                printk("delete on non-supporting F/W.\n");
3181
3182                                return (-EINVAL);
3183                        }
3184
3185                        rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3186
3187                        if( rval == 0 ) {
3188                                memset(&mc, 0, sizeof(megacmd_t));
3189
3190                                mc.status = rval;
3191
3192                                rval = mega_n_to_m((void __user *)arg, &mc);
3193                        }
3194
3195                        return rval;
3196                }
3197                /*
3198                 * This interface only support the regular passthru commands.
3199                 * Reject extended passthru and 64-bit passthru
3200                 */
3201                if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3202                        uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3203
3204                        printk(KERN_WARNING "megaraid: rejected passthru.\n");
3205
3206                        return (-EINVAL);
3207                }
3208
3209                /*
3210                 * For all internal commands, the buffer must be allocated in
3211                 * <4GB address range
3212                 */
3213                if( make_local_pdev(adapter, &pdev) != 0 )
3214                        return -EIO;
3215
3216                /* Is it a passthru command or a DCMD */
3217                if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3218                        /* Passthru commands */
3219
3220                        pthru = pci_alloc_consistent(pdev,
3221                                        sizeof(mega_passthru),
3222                                        &pthru_dma_hndl);
3223
3224                        if( pthru == NULL ) {
3225                                free_local_pdev(pdev);
3226                                return (-ENOMEM);
3227                        }
3228
3229                        /*
3230                         * The user passthru structure
3231                         */
3232                        upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3233
3234                        /*
3235                         * Copy in the user passthru here.
3236                         */
3237                        if( copy_from_user(pthru, upthru,
3238                                                sizeof(mega_passthru)) ) {
3239
3240                                pci_free_consistent(pdev,
3241                                                sizeof(mega_passthru), pthru,
3242                                                pthru_dma_hndl);
3243
3244                                free_local_pdev(pdev);
3245
3246                                return (-EFAULT);
3247                        }
3248
3249                        /*
3250                         * Is there a data transfer
3251                         */
3252                        if( pthru->dataxferlen ) {
3253                                data = pci_alloc_consistent(pdev,
3254                                                pthru->dataxferlen,
3255                                                &data_dma_hndl);
3256
3257                                if( data == NULL ) {
3258                                        pci_free_consistent(pdev,
3259                                                        sizeof(mega_passthru),
3260                                                        pthru,
3261                                                        pthru_dma_hndl);
3262
3263                                        free_local_pdev(pdev);
3264
3265                                        return (-ENOMEM);
3266                                }
3267
3268                                /*
3269                                 * Save the user address and point the kernel
3270                                 * address at just allocated memory
3271                                 */
3272                                uxferaddr = pthru->dataxferaddr;
3273                                pthru->dataxferaddr = data_dma_hndl;
3274                        }
3275
3276
3277                        /*
3278                         * Is data coming down-stream
3279                         */
3280                        if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3281                                /*
3282                                 * Get the user data
3283                                 */
3284                                if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3285                                                        pthru->dataxferlen) ) {
3286                                        rval = (-EFAULT);
3287                                        goto freemem_and_return;
3288                                }
3289                        }
3290
3291                        memset(&mc, 0, sizeof(megacmd_t));
3292
3293                        mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3294                        mc.xferaddr = (u32)pthru_dma_hndl;
3295
3296                        /*
3297                         * Issue the command
3298                         */
3299                        mega_internal_command(adapter, &mc, pthru);
3300
3301                        rval = mega_n_to_m((void __user *)arg, &mc);
3302
3303                        if( rval ) goto freemem_and_return;
3304
3305
3306                        /*
3307                         * Is data going up-stream
3308                         */
3309                        if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3310                                if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3311                                                        pthru->dataxferlen) ) {
3312                                        rval = (-EFAULT);
3313                                }
3314                        }
3315
3316                        /*
3317                         * Send the request sense data also, irrespective of
3318                         * whether the user has asked for it or not.
3319                         */
3320                        if (copy_to_user(upthru->reqsensearea,
3321                                        pthru->reqsensearea, 14))
3322                                rval = -EFAULT;
3323
3324freemem_and_return:
3325                        if( pthru->dataxferlen ) {
3326                                pci_free_consistent(pdev,
3327                                                pthru->dataxferlen, data,
3328                                                data_dma_hndl);
3329                        }
3330
3331                        pci_free_consistent(pdev, sizeof(mega_passthru),
3332                                        pthru, pthru_dma_hndl);
3333
3334                        free_local_pdev(pdev);
3335
3336                        return rval;
3337                }
3338                else {
3339                        /* DCMD commands */
3340
3341                        /*
3342                         * Is there a data transfer
3343                         */
3344                        if( uioc.xferlen ) {
3345                                data = pci_alloc_consistent(pdev,
3346                                                uioc.xferlen, &data_dma_hndl);
3347
3348                                if( data == NULL ) {
3349                                        free_local_pdev(pdev);
3350                                        return (-ENOMEM);
3351                                }
3352
3353                                uxferaddr = MBOX(uioc)->xferaddr;
3354                        }
3355
3356                        /*
3357                         * Is data coming down-stream
3358                         */
3359                        if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3360                                /*
3361                                 * Get the user data
3362                                 */
3363                                if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3364                                                        uioc.xferlen) ) {
3365
3366                                        pci_free_consistent(pdev,
3367                                                        uioc.xferlen,
3368                                                        data, data_dma_hndl);
3369
3370                                        free_local_pdev(pdev);
3371
3372                                        return (-EFAULT);
3373                                }
3374                        }
3375
3376                        memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3377
3378                        mc.xferaddr = (u32)data_dma_hndl;
3379
3380                        /*
3381                         * Issue the command
3382                         */
3383                        mega_internal_command(adapter, &mc, NULL);
3384
3385                        rval = mega_n_to_m((void __user *)arg, &mc);
3386
3387                        if( rval ) {
3388                                if( uioc.xferlen ) {
3389                                        pci_free_consistent(pdev,
3390                                                        uioc.xferlen, data,
3391                                                        data_dma_hndl);
3392                                }
3393
3394                                free_local_pdev(pdev);
3395
3396                                return rval;
3397                        }
3398
3399                        /*
3400                         * Is data going up-stream
3401                         */
3402                        if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3403                                if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3404                                                        uioc.xferlen) ) {
3405
3406                                        rval = (-EFAULT);
3407                                }
3408                        }
3409
3410                        if( uioc.xferlen ) {
3411                                pci_free_consistent(pdev,
3412                                                uioc.xferlen, data,
3413                                                data_dma_hndl);
3414                        }
3415
3416                        free_local_pdev(pdev);
3417
3418                        return rval;
3419                }
3420
3421        default:
3422                return (-EINVAL);
3423        }
3424
3425        return 0;
3426}
3427
3428static long
3429megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3430{
3431        int ret;
3432
3433        mutex_lock(&megadev_mutex);
3434        ret = megadev_ioctl(filep, cmd, arg);
3435        mutex_unlock(&megadev_mutex);
3436
3437        return ret;
3438}
3439
3440/**
3441 * mega_m_to_n()
3442 * @arg - user address
3443 * @uioc - new ioctl structure
3444 *
3445 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3446 * structure
3447 *
3448 * Converts the older mimd ioctl structure to newer NIT structure
3449 */
3450static int
3451mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3452{
3453        struct uioctl_t uioc_mimd;
3454        char    signature[8] = {0};
3455        u8      opcode;
3456        u8      subopcode;
3457
3458
3459        /*
3460         * check is the application conforms to NIT. We do not have to do much
3461         * in that case.
3462         * We exploit the fact that the signature is stored in the very
3463         * beginning of the structure.
3464         */
3465
3466        if( copy_from_user(signature, arg, 7) )
3467                return (-EFAULT);
3468
3469        if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3470
3471                /*
3472                 * NOTE NOTE: The nit ioctl is still under flux because of
3473                 * change of mailbox definition, in HPE. No applications yet
3474                 * use this interface and let's not have applications use this
3475                 * interface till the new specifitions are in place.
3476                 */
3477                return -EINVAL;
3478#if 0
3479                if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3480                        return (-EFAULT);
3481                return 0;
3482#endif
3483        }
3484
3485        /*
3486         * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3487         *
3488         * Get the user ioctl structure
3489         */
3490        if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3491                return (-EFAULT);
3492
3493
3494        /*
3495         * Get the opcode and subopcode for the commands
3496         */
3497        opcode = uioc_mimd.ui.fcs.opcode;
3498        subopcode = uioc_mimd.ui.fcs.subopcode;
3499
3500        switch (opcode) {
3501        case 0x82:
3502
3503                switch (subopcode) {
3504
3505                case MEGAIOC_QDRVRVER:  /* Query driver version */
3506                        uioc->opcode = GET_DRIVER_VER;
3507                        uioc->uioc_uaddr = uioc_mimd.data;
3508                        break;
3509
3510                case MEGAIOC_QNADAP:    /* Get # of adapters */
3511                        uioc->opcode = GET_N_ADAP;
3512                        uioc->uioc_uaddr = uioc_mimd.data;
3513                        break;
3514
3515                case MEGAIOC_QADAPINFO: /* Get adapter information */
3516                        uioc->opcode = GET_ADAP_INFO;
3517                        uioc->adapno = uioc_mimd.ui.fcs.adapno;
3518                        uioc->uioc_uaddr = uioc_mimd.data;
3519                        break;
3520
3521                default:
3522                        return(-EINVAL);
3523                }
3524
3525                break;
3526
3527
3528        case 0x81:
3529
3530                uioc->opcode = MBOX_CMD;
3531                uioc->adapno = uioc_mimd.ui.fcs.adapno;
3532
3533                memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3534
3535                uioc->xferlen = uioc_mimd.ui.fcs.length;
3536
3537                if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3538                if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3539
3540                break;
3541
3542        case 0x80:
3543
3544                uioc->opcode = MBOX_CMD;
3545                uioc->adapno = uioc_mimd.ui.fcs.adapno;
3546
3547                memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3548
3549                /*
3550                 * Choose the xferlen bigger of input and output data
3551                 */
3552                uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3553                        uioc_mimd.outlen : uioc_mimd.inlen;
3554
3555                if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3556                if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3557
3558                break;
3559
3560        default:
3561                return (-EINVAL);
3562
3563        }
3564
3565        return 0;
3566}
3567
3568/*
3569 * mega_n_to_m()
3570 * @arg - user address
3571 * @mc - mailbox command
3572 *
3573 * Updates the status information to the application, depending on application
3574 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3575 */
3576static int
3577mega_n_to_m(void __user *arg, megacmd_t *mc)
3578{
3579        nitioctl_t      __user *uiocp;
3580        megacmd_t       __user *umc;
3581        mega_passthru   __user *upthru;
3582        struct uioctl_t __user *uioc_mimd;
3583        char    signature[8] = {0};
3584
3585        /*
3586         * check is the application conforms to NIT.
3587         */
3588        if( copy_from_user(signature, arg, 7) )
3589                return -EFAULT;
3590
3591        if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3592
3593                uiocp = arg;
3594
3595                if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3596                        return (-EFAULT);
3597
3598                if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3599
3600                        umc = MBOX_P(uiocp);
3601
3602                        if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3603                                return -EFAULT;
3604
3605                        if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3606                                return (-EFAULT);
3607                }
3608        }
3609        else {
3610                uioc_mimd = arg;
3611
3612                if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3613                        return (-EFAULT);
3614
3615                if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3616
3617                        umc = (megacmd_t __user *)uioc_mimd->mbox;
3618
3619                        if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3620                                return (-EFAULT);
3621
3622                        if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3623                                return (-EFAULT);
3624                }
3625        }
3626
3627        return 0;
3628}
3629
3630
3631/*
3632 * MEGARAID 'FW' commands.
3633 */
3634
3635/**
3636 * mega_is_bios_enabled()
3637 * @adapter - pointer to our soft state
3638 *
3639 * issue command to find out if the BIOS is enabled for this controller
3640 */
3641static int
3642mega_is_bios_enabled(adapter_t *adapter)
3643{
3644        unsigned char   raw_mbox[sizeof(struct mbox_out)];
3645        mbox_t  *mbox;
3646        int     ret;
3647
3648        mbox = (mbox_t *)raw_mbox;
3649
3650        memset(&mbox->m_out, 0, sizeof(raw_mbox));
3651
3652        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3653
3654        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3655
3656        raw_mbox[0] = IS_BIOS_ENABLED;
3657        raw_mbox[2] = GET_BIOS;
3658
3659
3660        ret = issue_scb_block(adapter, raw_mbox);
3661
3662        return *(char *)adapter->mega_buffer;
3663}
3664
3665
3666/**
3667 * mega_enum_raid_scsi()
3668 * @adapter - pointer to our soft state
3669 *
3670 * Find out what channels are RAID/SCSI. This information is used to
3671 * differentiate the virtual channels and physical channels and to support
3672 * ROMB feature and non-disk devices.
3673 */
3674static void
3675mega_enum_raid_scsi(adapter_t *adapter)
3676{
3677        unsigned char raw_mbox[sizeof(struct mbox_out)];
3678        mbox_t *mbox;
3679        int i;
3680
3681        mbox = (mbox_t *)raw_mbox;
3682
3683        memset(&mbox->m_out, 0, sizeof(raw_mbox));
3684
3685        /*
3686         * issue command to find out what channels are raid/scsi
3687         */
3688        raw_mbox[0] = CHNL_CLASS;
3689        raw_mbox[2] = GET_CHNL_CLASS;
3690
3691        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3692
3693        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3694
3695        /*
3696         * Non-ROMB firmware fail this command, so all channels
3697         * must be shown RAID
3698         */
3699        adapter->mega_ch_class = 0xFF;
3700
3701        if(!issue_scb_block(adapter, raw_mbox)) {
3702                adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3703
3704        }
3705
3706        for( i = 0; i < adapter->product_info.nchannels; i++ ) { 
3707                if( (adapter->mega_ch_class >> i) & 0x01 ) {
3708                        printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3709                                        i);
3710                }
3711                else {
3712                        printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3713                                        i);
3714                }
3715        }
3716
3717        return;
3718}
3719
3720
3721/**
3722 * mega_get_boot_drv()
3723 * @adapter - pointer to our soft state
3724 *
3725 * Find out which device is the boot device. Note, any logical drive or any
3726 * phyical device (e.g., a CDROM) can be designated as a boot device.
3727 */
3728static void
3729mega_get_boot_drv(adapter_t *adapter)
3730{
3731        struct private_bios_data        *prv_bios_data;
3732        unsigned char   raw_mbox[sizeof(struct mbox_out)];
3733        mbox_t  *mbox;
3734        u16     cksum = 0;
3735        u8      *cksum_p;
3736        u8      boot_pdrv;
3737        int     i;
3738
3739        mbox = (mbox_t *)raw_mbox;
3740
3741        memset(&mbox->m_out, 0, sizeof(raw_mbox));
3742
3743        raw_mbox[0] = BIOS_PVT_DATA;
3744        raw_mbox[2] = GET_BIOS_PVT_DATA;
3745
3746        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3747
3748        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3749
3750        adapter->boot_ldrv_enabled = 0;
3751        adapter->boot_ldrv = 0;
3752
3753        adapter->boot_pdrv_enabled = 0;
3754        adapter->boot_pdrv_ch = 0;
3755        adapter->boot_pdrv_tgt = 0;
3756
3757        if(issue_scb_block(adapter, raw_mbox) == 0) {
3758                prv_bios_data =
3759                        (struct private_bios_data *)adapter->mega_buffer;
3760
3761                cksum = 0;
3762                cksum_p = (char *)prv_bios_data;
3763                for (i = 0; i < 14; i++ ) {
3764                        cksum += (u16)(*cksum_p++);
3765                }
3766
3767                if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3768
3769                        /*
3770                         * If MSB is set, a physical drive is set as boot
3771                         * device
3772                         */
3773                        if( prv_bios_data->boot_drv & 0x80 ) {
3774                                adapter->boot_pdrv_enabled = 1;
3775                                boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3776                                adapter->boot_pdrv_ch = boot_pdrv / 16;
3777                                adapter->boot_pdrv_tgt = boot_pdrv % 16;
3778                        }
3779                        else {
3780                                adapter->boot_ldrv_enabled = 1;
3781                                adapter->boot_ldrv = prv_bios_data->boot_drv;
3782                        }
3783                }
3784        }
3785
3786}
3787
3788/**
3789 * mega_support_random_del()
3790 * @adapter - pointer to our soft state
3791 *
3792 * Find out if this controller supports random deletion and addition of
3793 * logical drives
3794 */
3795static int
3796mega_support_random_del(adapter_t *adapter)
3797{
3798        unsigned char raw_mbox[sizeof(struct mbox_out)];
3799        mbox_t *mbox;
3800        int rval;
3801
3802        mbox = (mbox_t *)raw_mbox;
3803
3804        memset(&mbox->m_out, 0, sizeof(raw_mbox));
3805
3806        /*
3807         * issue command
3808         */
3809        raw_mbox[0] = FC_DEL_LOGDRV;
3810        raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3811
3812        rval = issue_scb_block(adapter, raw_mbox);
3813
3814        return !rval;
3815}
3816
3817
3818/**
3819 * mega_support_ext_cdb()
3820 * @adapter - pointer to our soft state
3821 *
3822 * Find out if this firmware support cdblen > 10
3823 */
3824static int
3825mega_support_ext_cdb(adapter_t *adapter)
3826{
3827        unsigned char raw_mbox[sizeof(struct mbox_out)];
3828        mbox_t *mbox;
3829        int rval;
3830
3831        mbox = (mbox_t *)raw_mbox;
3832
3833        memset(&mbox->m_out, 0, sizeof(raw_mbox));
3834        /*
3835         * issue command to find out if controller supports extended CDBs.
3836         */
3837        raw_mbox[0] = 0xA4;
3838        raw_mbox[2] = 0x16;
3839
3840        rval = issue_scb_block(adapter, raw_mbox);
3841
3842        return !rval;
3843}
3844
3845
3846/**
3847 * mega_del_logdrv()
3848 * @adapter - pointer to our soft state
3849 * @logdrv - logical drive to be deleted
3850 *
3851 * Delete the specified logical drive. It is the responsibility of the user
3852 * app to let the OS know about this operation.
3853 */
3854static int
3855mega_del_logdrv(adapter_t *adapter, int logdrv)
3856{
3857        unsigned long flags;
3858        scb_t *scb;
3859        int rval;
3860
3861        /*
3862         * Stop sending commands to the controller, queue them internally.
3863         * When deletion is complete, ISR will flush the queue.
3864         */
3865        atomic_set(&adapter->quiescent, 1);
3866
3867        /*
3868         * Wait till all the issued commands are complete and there are no
3869         * commands in the pending queue
3870         */
3871        while (atomic_read(&adapter->pend_cmds) > 0 ||
3872               !list_empty(&adapter->pending_list))
3873                msleep(1000);   /* sleep for 1s */
3874
3875        rval = mega_do_del_logdrv(adapter, logdrv);
3876
3877        spin_lock_irqsave(&adapter->lock, flags);
3878
3879        /*
3880         * If delete operation was successful, add 0x80 to the logical drive
3881         * ids for commands in the pending queue.
3882         */
3883        if (adapter->read_ldidmap) {
3884                struct list_head *pos;
3885                list_for_each(pos, &adapter->pending_list) {
3886                        scb = list_entry(pos, scb_t, list);
3887                        if (scb->pthru->logdrv < 0x80 )
3888                                scb->pthru->logdrv += 0x80;
3889                }
3890        }
3891
3892        atomic_set(&adapter->quiescent, 0);
3893
3894        mega_runpendq(adapter);
3895
3896        spin_unlock_irqrestore(&adapter->lock, flags);
3897
3898        return rval;
3899}
3900
3901
3902static int
3903mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3904{
3905        megacmd_t       mc;
3906        int     rval;
3907
3908        memset( &mc, 0, sizeof(megacmd_t));
3909
3910        mc.cmd = FC_DEL_LOGDRV;
3911        mc.opcode = OP_DEL_LOGDRV;
3912        mc.subopcode = logdrv;
3913
3914        rval = mega_internal_command(adapter, &mc, NULL);
3915
3916        /* log this event */
3917        if(rval) {
3918                printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
3919                return rval;
3920        }
3921
3922        /*
3923         * After deleting first logical drive, the logical drives must be
3924         * addressed by adding 0x80 to the logical drive id.
3925         */
3926        adapter->read_ldidmap = 1;
3927
3928        return rval;
3929}
3930
3931
3932/**
3933 * mega_get_max_sgl()
3934 * @adapter - pointer to our soft state
3935 *
3936 * Find out the maximum number of scatter-gather elements supported by this
3937 * version of the firmware
3938 */
3939static void
3940mega_get_max_sgl(adapter_t *adapter)
3941{
3942        unsigned char   raw_mbox[sizeof(struct mbox_out)];
3943        mbox_t  *mbox;
3944
3945        mbox = (mbox_t *)raw_mbox;
3946
3947        memset(mbox, 0, sizeof(raw_mbox));
3948
3949        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3950
3951        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3952
3953        raw_mbox[0] = MAIN_MISC_OPCODE;
3954        raw_mbox[2] = GET_MAX_SG_SUPPORT;
3955
3956
3957        if( issue_scb_block(adapter, raw_mbox) ) {
3958                /*
3959                 * f/w does not support this command. Choose the default value
3960                 */
3961                adapter->sglen = MIN_SGLIST;
3962        }
3963        else {
3964                adapter->sglen = *((char *)adapter->mega_buffer);
3965                
3966                /*
3967                 * Make sure this is not more than the resources we are
3968                 * planning to allocate
3969                 */
3970                if ( adapter->sglen > MAX_SGLIST )
3971                        adapter->sglen = MAX_SGLIST;
3972        }
3973
3974        return;
3975}
3976
3977
3978/**
3979 * mega_support_cluster()
3980 * @adapter - pointer to our soft state
3981 *
3982 * Find out if this firmware support cluster calls.
3983 */
3984static int
3985mega_support_cluster(adapter_t *adapter)
3986{
3987        unsigned char   raw_mbox[sizeof(struct mbox_out)];
3988        mbox_t  *mbox;
3989
3990        mbox = (mbox_t *)raw_mbox;
3991
3992        memset(mbox, 0, sizeof(raw_mbox));
3993
3994        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3995
3996        mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3997
3998        /*
3999         * Try to get the initiator id. This command will succeed iff the
4000         * clustering is available on this HBA.
4001         */
4002        raw_mbox[0] = MEGA_GET_TARGET_ID;
4003
4004        if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4005
4006                /*
4007                 * Cluster support available. Get the initiator target id.
4008                 * Tell our id to mid-layer too.
4009                 */
4010                adapter->this_id = *(u32 *)adapter->mega_buffer;
4011                adapter->host->this_id = adapter->this_id;
4012
4013                return 1;
4014        }
4015
4016        return 0;
4017}
4018
4019#ifdef CONFIG_PROC_FS
4020/**
4021 * mega_adapinq()
4022 * @adapter - pointer to our soft state
4023 * @dma_handle - DMA address of the buffer
4024 *
4025 * Issue internal commands while interrupts are available.
4026 * We only issue direct mailbox commands from within the driver. ioctl()
4027 * interface using these routines can issue passthru commands.
4028 */
4029static int
4030mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4031{
4032        megacmd_t       mc;
4033
4034        memset(&mc, 0, sizeof(megacmd_t));
4035
4036        if( adapter->flag & BOARD_40LD ) {
4037                mc.cmd = FC_NEW_CONFIG;
4038                mc.opcode = NC_SUBOP_ENQUIRY3;
4039                mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4040        }
4041        else {
4042                mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4043        }
4044
4045        mc.xferaddr = (u32)dma_handle;
4046
4047        if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4048                return -1;
4049        }
4050
4051        return 0;
4052}
4053
4054
4055/** mega_internal_dev_inquiry()
4056 * @adapter - pointer to our soft state
4057 * @ch - channel for this device
4058 * @tgt - ID of this device
4059 * @buf_dma_handle - DMA address of the buffer
4060 *
4061 * Issue the scsi inquiry for the specified device.
4062 */
4063static int
4064mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4065                dma_addr_t buf_dma_handle)
4066{
4067        mega_passthru   *pthru;
4068        dma_addr_t      pthru_dma_handle;
4069        megacmd_t       mc;
4070        int             rval;
4071        struct pci_dev  *pdev;
4072
4073
4074        /*
4075         * For all internal commands, the buffer must be allocated in <4GB
4076         * address range
4077         */
4078        if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4079
4080        pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4081                        &pthru_dma_handle);
4082
4083        if( pthru == NULL ) {
4084                free_local_pdev(pdev);
4085                return -1;
4086        }
4087
4088        pthru->timeout = 2;
4089        pthru->ars = 1;
4090        pthru->reqsenselen = 14;
4091        pthru->islogical = 0;
4092
4093        pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4094
4095        pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4096
4097        pthru->cdblen = 6;
4098
4099        pthru->cdb[0] = INQUIRY;
4100        pthru->cdb[1] = 0;
4101        pthru->cdb[2] = 0;
4102        pthru->cdb[3] = 0;
4103        pthru->cdb[4] = 255;
4104        pthru->cdb[5] = 0;
4105
4106
4107        pthru->dataxferaddr = (u32)buf_dma_handle;
4108        pthru->dataxferlen = 256;
4109
4110        memset(&mc, 0, sizeof(megacmd_t));
4111
4112        mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4113        mc.xferaddr = (u32)pthru_dma_handle;
4114
4115        rval = mega_internal_command(adapter, &mc, pthru);
4116
4117        pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4118                        pthru_dma_handle);
4119
4120        free_local_pdev(pdev);
4121
4122        return rval;
4123}
4124#endif
4125
4126/**
4127 * mega_internal_command()
4128 * @adapter - pointer to our soft state
4129 * @mc - the mailbox command
4130 * @pthru - Passthru structure for DCDB commands
4131 *
4132 * Issue the internal commands in interrupt mode.
4133 * The last argument is the address of the passthru structure if the command
4134 * to be fired is a passthru command
4135 *
4136 * lockscope specifies whether the caller has already acquired the lock. Of
4137 * course, the caller must know which lock we are talking about.
4138 *
4139 * Note: parameter 'pthru' is null for non-passthru commands.
4140 */
4141static int
4142mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4143{
4144        Scsi_Cmnd       *scmd;
4145        struct  scsi_device *sdev;
4146        scb_t   *scb;
4147        int     rval;
4148
4149        scmd = scsi_allocate_command(GFP_KERNEL);
4150        if (!scmd)
4151                return -ENOMEM;
4152
4153        /*
4154         * The internal commands share one command id and hence are
4155         * serialized. This is so because we want to reserve maximum number of
4156         * available command ids for the I/O commands.
4157         */
4158        mutex_lock(&adapter->int_mtx);
4159
4160        scb = &adapter->int_scb;
4161        memset(scb, 0, sizeof(scb_t));
4162
4163        sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4164        scmd->device = sdev;
4165
4166        memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4167        scmd->cmnd = adapter->int_cdb;
4168        scmd->device->host = adapter->host;
4169        scmd->host_scribble = (void *)scb;
4170        scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4171
4172        scb->state |= SCB_ACTIVE;
4173        scb->cmd = scmd;
4174
4175        memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4176
4177        /*
4178         * Is it a passthru command
4179         */
4180        if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4181
4182                scb->pthru = pthru;
4183        }
4184
4185        scb->idx = CMDID_INT_CMDS;
4186
4187        megaraid_queue_lck(scmd, mega_internal_done);
4188
4189        wait_for_completion(&adapter->int_waitq);
4190
4191        rval = scmd->result;
4192        mc->status = scmd->result;
4193        kfree(sdev);
4194
4195        /*
4196         * Print a debug message for all failed commands. Applications can use
4197         * this information.
4198         */
4199        if( scmd->result && trace_level ) {
4200                printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4201                        mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4202        }
4203
4204        mutex_unlock(&adapter->int_mtx);
4205
4206        scsi_free_command(GFP_KERNEL, scmd);
4207
4208        return rval;
4209}
4210
4211
4212/**
4213 * mega_internal_done()
4214 * @scmd - internal scsi command
4215 *
4216 * Callback routine for internal commands.
4217 */
4218static void
4219mega_internal_done(Scsi_Cmnd *scmd)
4220{
4221        adapter_t       *adapter;
4222
4223        adapter = (adapter_t *)scmd->device->host->hostdata;
4224
4225        complete(&adapter->int_waitq);
4226
4227}
4228
4229
4230static struct scsi_host_template megaraid_template = {
4231        .module                         = THIS_MODULE,
4232        .name                           = "MegaRAID",
4233        .proc_name                      = "megaraid_legacy",
4234        .info                           = megaraid_info,
4235        .queuecommand                   = megaraid_queue,       
4236        .bios_param                     = megaraid_biosparam,
4237        .max_sectors                    = MAX_SECTORS_PER_IO,
4238        .can_queue                      = MAX_COMMANDS,
4239        .this_id                        = DEFAULT_INITIATOR_ID,
4240        .sg_tablesize                   = MAX_SGLIST,
4241        .cmd_per_lun                    = DEF_CMD_PER_LUN,
4242        .use_clustering                 = ENABLE_CLUSTERING,
4243        .eh_abort_handler               = megaraid_abort,
4244        .eh_device_reset_handler        = megaraid_reset,
4245        .eh_bus_reset_handler           = megaraid_reset,
4246        .eh_host_reset_handler          = megaraid_reset,
4247};
4248
4249static int
4250megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4251{
4252        struct Scsi_Host *host;
4253        adapter_t *adapter;
4254        unsigned long mega_baseport, tbase, flag = 0;
4255        u16 subsysid, subsysvid;
4256        u8 pci_bus, pci_dev_func;
4257        int irq, i, j;
4258        int error = -ENODEV;
4259
4260        if (pci_enable_device(pdev))
4261                goto out;
4262        pci_set_master(pdev);
4263
4264        pci_bus = pdev->bus->number;
4265        pci_dev_func = pdev->devfn;
4266
4267        /*
4268         * The megaraid3 stuff reports the ID of the Intel part which is not
4269         * remotely specific to the megaraid
4270         */
4271        if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4272                u16 magic;
4273                /*
4274                 * Don't fall over the Compaq management cards using the same
4275                 * PCI identifier
4276                 */
4277                if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4278                    pdev->subsystem_device == 0xC000)
4279                        return -ENODEV;
4280                /* Now check the magic signature byte */
4281                pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4282                if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4283                        return -ENODEV;
4284                /* Ok it is probably a megaraid */
4285        }
4286
4287        /*
4288         * For these vendor and device ids, signature offsets are not
4289         * valid and 64 bit is implicit
4290         */
4291        if (id->driver_data & BOARD_64BIT)
4292                flag |= BOARD_64BIT;
4293        else {
4294                u32 magic64;
4295
4296                pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4297                if (magic64 == HBA_SIGNATURE_64BIT)
4298                        flag |= BOARD_64BIT;
4299        }
4300
4301        subsysvid = pdev->subsystem_vendor;
4302        subsysid = pdev->subsystem_device;
4303
4304        printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4305                id->vendor, id->device, pci_bus);
4306
4307        printk("slot %d:func %d\n",
4308                PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4309
4310        /* Read the base port and IRQ from PCI */
4311        mega_baseport = pci_resource_start(pdev, 0);
4312        irq = pdev->irq;
4313
4314        tbase = mega_baseport;
4315        if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4316                flag |= BOARD_MEMMAP;
4317
4318                if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4319                        printk(KERN_WARNING "megaraid: mem region busy!\n");
4320                        goto out_disable_device;
4321                }
4322
4323                mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4324                if (!mega_baseport) {
4325                        printk(KERN_WARNING
4326                               "megaraid: could not map hba memory\n");
4327                        goto out_release_region;
4328                }
4329        } else {
4330                flag |= BOARD_IOMAP;
4331                mega_baseport += 0x10;
4332
4333                if (!request_region(mega_baseport, 16, "megaraid"))
4334                        goto out_disable_device;
4335        }
4336
4337        /* Initialize SCSI Host structure */
4338        host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4339        if (!host)
4340                goto out_iounmap;
4341
4342        adapter = (adapter_t *)host->hostdata;
4343        memset(adapter, 0, sizeof(adapter_t));
4344
4345        printk(KERN_NOTICE
4346                "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4347                host->host_no, mega_baseport, irq);
4348
4349        adapter->base = mega_baseport;
4350        if (flag & BOARD_MEMMAP)
4351                adapter->mmio_base = (void __iomem *) mega_baseport;
4352
4353        INIT_LIST_HEAD(&adapter->free_list);
4354        INIT_LIST_HEAD(&adapter->pending_list);
4355        INIT_LIST_HEAD(&adapter->completed_list);
4356
4357        adapter->flag = flag;
4358        spin_lock_init(&adapter->lock);
4359
4360        host->cmd_per_lun = max_cmd_per_lun;
4361        host->max_sectors = max_sectors_per_io;
4362
4363        adapter->dev = pdev;
4364        adapter->host = host;
4365
4366        adapter->host->irq = irq;
4367
4368        if (flag & BOARD_MEMMAP)
4369                adapter->host->base = tbase;
4370        else {
4371                adapter->host->io_port = tbase;
4372                adapter->host->n_io_port = 16;
4373        }
4374
4375        adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4376
4377        /*
4378         * Allocate buffer to issue internal commands.
4379         */
4380        adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4381                MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4382        if (!adapter->mega_buffer) {
4383                printk(KERN_WARNING "megaraid: out of RAM.\n");
4384                goto out_host_put;
4385        }
4386
4387        adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4388        if (!adapter->scb_list) {
4389                printk(KERN_WARNING "megaraid: out of RAM.\n");
4390                goto out_free_cmd_buffer;
4391        }
4392
4393        if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4394                                megaraid_isr_memmapped : megaraid_isr_iomapped,
4395                                        IRQF_SHARED, "megaraid", adapter)) {
4396                printk(KERN_WARNING
4397                        "megaraid: Couldn't register IRQ %d!\n", irq);
4398                goto out_free_scb_list;
4399        }
4400
4401        if (mega_setup_mailbox(adapter))
4402                goto out_free_irq;
4403
4404        if (mega_query_adapter(adapter))
4405                goto out_free_mbox;
4406
4407        /*
4408         * Have checks for some buggy f/w
4409         */
4410        if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4411                /*
4412                 * Which firmware
4413                 */
4414                if (!strcmp(adapter->fw_version, "3.00") ||
4415                                !strcmp(adapter->fw_version, "3.01")) {
4416
4417                        printk( KERN_WARNING
4418                                "megaraid: Your  card is a Dell PERC "
4419                                "2/SC RAID controller with  "
4420                                "firmware\nmegaraid: 3.00 or 3.01.  "
4421                                "This driver is known to have "
4422                                "corruption issues\nmegaraid: with "
4423                                "those firmware versions on this "
4424                                "specific card.  In order\nmegaraid: "
4425                                "to protect your data, please upgrade "
4426                                "your firmware to version\nmegaraid: "
4427                                "3.10 or later, available from the "
4428                                "Dell Technical Support web\n"
4429                                "megaraid: site at\nhttp://support."
4430                                "dell.com/us/en/filelib/download/"
4431                                "index.asp?fileid=2940\n"
4432                        );
4433                }
4434        }
4435
4436        /*
4437         * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4438         * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4439         * support, since this firmware cannot handle 64 bit
4440         * addressing
4441         */
4442        if ((subsysvid == PCI_VENDOR_ID_HP) &&
4443            ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4444                /*
4445                 * which firmware
4446                 */
4447                if (!strcmp(adapter->fw_version, "H01.07") ||
4448                    !strcmp(adapter->fw_version, "H01.08") ||
4449                    !strcmp(adapter->fw_version, "H01.09") ) {
4450                        printk(KERN_WARNING
4451                                "megaraid: Firmware H.01.07, "
4452                                "H.01.08, and H.01.09 on 1M/2M "
4453                                "controllers\n"
4454                                "megaraid: do not support 64 bit "
4455                                "addressing.\nmegaraid: DISABLING "
4456                                "64 bit support.\n");
4457                        adapter->flag &= ~BOARD_64BIT;
4458                }
4459        }
4460
4461        if (mega_is_bios_enabled(adapter))
4462                mega_hbas[hba_count].is_bios_enabled = 1;
4463        mega_hbas[hba_count].hostdata_addr = adapter;
4464
4465        /*
4466         * Find out which channel is raid and which is scsi. This is
4467         * for ROMB support.
4468         */
4469        mega_enum_raid_scsi(adapter);
4470
4471        /*
4472         * Find out if a logical drive is set as the boot drive. If
4473         * there is one, will make that as the first logical drive.
4474         * ROMB: Do we have to boot from a physical drive. Then all
4475         * the physical drives would appear before the logical disks.
4476         * Else, all the physical drives would be exported to the mid
4477         * layer after logical drives.
4478         */
4479        mega_get_boot_drv(adapter);
4480
4481        if (adapter->boot_pdrv_enabled) {
4482                j = adapter->product_info.nchannels;
4483                for( i = 0; i < j; i++ )
4484                        adapter->logdrv_chan[i] = 0;
4485                for( i = j; i < NVIRT_CHAN + j; i++ )
4486                        adapter->logdrv_chan[i] = 1;
4487        } else {
4488                for (i = 0; i < NVIRT_CHAN; i++)
4489                        adapter->logdrv_chan[i] = 1;
4490                for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4491                        adapter->logdrv_chan[i] = 0;
4492                adapter->mega_ch_class <<= NVIRT_CHAN;
4493        }
4494
4495        /*
4496         * Do we support random deletion and addition of logical
4497         * drives
4498         */
4499        adapter->read_ldidmap = 0;      /* set it after first logdrv
4500                                                   delete cmd */
4501        adapter->support_random_del = mega_support_random_del(adapter);
4502
4503        /* Initialize SCBs */
4504        if (mega_init_scb(adapter))
4505                goto out_free_mbox;
4506
4507        /*
4508         * Reset the pending commands counter
4509         */
4510        atomic_set(&adapter->pend_cmds, 0);
4511
4512        /*
4513         * Reset the adapter quiescent flag
4514         */
4515        atomic_set(&adapter->quiescent, 0);
4516
4517        hba_soft_state[hba_count] = adapter;
4518
4519        /*
4520         * Fill in the structure which needs to be passed back to the
4521         * application when it does an ioctl() for controller related
4522         * information.
4523         */
4524        i = hba_count;
4525
4526        mcontroller[i].base = mega_baseport;
4527        mcontroller[i].irq = irq;
4528        mcontroller[i].numldrv = adapter->numldrv;
4529        mcontroller[i].pcibus = pci_bus;
4530        mcontroller[i].pcidev = id->device;
4531        mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4532        mcontroller[i].pciid = -1;
4533        mcontroller[i].pcivendor = id->vendor;
4534        mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4535        mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4536
4537
4538        /* Set the Mode of addressing to 64 bit if we can */
4539        if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4540                pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4541                adapter->has_64bit_addr = 1;
4542        } else  {
4543                pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4544                adapter->has_64bit_addr = 0;
4545        }
4546                
4547        mutex_init(&adapter->int_mtx);
4548        init_completion(&adapter->int_waitq);
4549
4550        adapter->this_id = DEFAULT_INITIATOR_ID;
4551        adapter->host->this_id = DEFAULT_INITIATOR_ID;
4552
4553#if MEGA_HAVE_CLUSTERING
4554        /*
4555         * Is cluster support enabled on this controller
4556         * Note: In a cluster the HBAs ( the initiators ) will have
4557         * different target IDs and we cannot assume it to be 7. Call
4558         * to mega_support_cluster() will get the target ids also if
4559         * the cluster support is available
4560         */
4561        adapter->has_cluster = mega_support_cluster(adapter);
4562        if (adapter->has_cluster) {
4563                printk(KERN_NOTICE
4564                        "megaraid: Cluster driver, initiator id:%d\n",
4565                        adapter->this_id);
4566        }
4567#endif
4568
4569        pci_set_drvdata(pdev, host);
4570
4571        mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4572
4573        error = scsi_add_host(host, &pdev->dev);
4574        if (error)
4575                goto out_free_mbox;
4576
4577        scsi_scan_host(host);
4578        hba_count++;
4579        return 0;
4580
4581 out_free_mbox:
4582        pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4583                        adapter->una_mbox64, adapter->una_mbox64_dma);
4584 out_free_irq:
4585        free_irq(adapter->host->irq, adapter);
4586 out_free_scb_list:
4587        kfree(adapter->scb_list);
4588 out_free_cmd_buffer:
4589        pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4590                        adapter->mega_buffer, adapter->buf_dma_handle);
4591 out_host_put:
4592        scsi_host_put(host);
4593 out_iounmap:
4594        if (flag & BOARD_MEMMAP)
4595                iounmap((void *)mega_baseport);
4596 out_release_region:
4597        if (flag & BOARD_MEMMAP)
4598                release_mem_region(tbase, 128);
4599        else
4600                release_region(mega_baseport, 16);
4601 out_disable_device:
4602        pci_disable_device(pdev);
4603 out:
4604        return error;
4605}
4606
4607static void
4608__megaraid_shutdown(adapter_t *adapter)
4609{
4610        u_char  raw_mbox[sizeof(struct mbox_out)];
4611        mbox_t  *mbox = (mbox_t *)raw_mbox;
4612        int     i;
4613
4614        /* Flush adapter cache */
4615        memset(&mbox->m_out, 0, sizeof(raw_mbox));
4616        raw_mbox[0] = FLUSH_ADAPTER;
4617
4618        free_irq(adapter->host->irq, adapter);
4619
4620        /* Issue a blocking (interrupts disabled) command to the card */
4621        issue_scb_block(adapter, raw_mbox);
4622
4623        /* Flush disks cache */
4624        memset(&mbox->m_out, 0, sizeof(raw_mbox));
4625        raw_mbox[0] = FLUSH_SYSTEM;
4626
4627        /* Issue a blocking (interrupts disabled) command to the card */
4628        issue_scb_block(adapter, raw_mbox);
4629        
4630        if (atomic_read(&adapter->pend_cmds) > 0)
4631                printk(KERN_WARNING "megaraid: pending commands!!\n");
4632
4633        /*
4634         * Have a delibrate delay to make sure all the caches are
4635         * actually flushed.
4636         */
4637        for (i = 0; i <= 10; i++)
4638                mdelay(1000);
4639}
4640
4641static void
4642megaraid_remove_one(struct pci_dev *pdev)
4643{
4644        struct Scsi_Host *host = pci_get_drvdata(pdev);
4645        adapter_t *adapter = (adapter_t *)host->hostdata;
4646
4647        scsi_remove_host(host);
4648
4649        __megaraid_shutdown(adapter);
4650
4651        /* Free our resources */
4652        if (adapter->flag & BOARD_MEMMAP) {
4653                iounmap((void *)adapter->base);
4654                release_mem_region(adapter->host->base, 128);
4655        } else
4656                release_region(adapter->base, 16);
4657
4658        mega_free_sgl(adapter);
4659
4660#ifdef CONFIG_PROC_FS
4661        if (adapter->controller_proc_dir_entry) {
4662                remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4663                remove_proc_entry("config",
4664                                adapter->controller_proc_dir_entry);
4665                remove_proc_entry("mailbox",
4666                                adapter->controller_proc_dir_entry);
4667#if MEGA_HAVE_ENH_PROC
4668                remove_proc_entry("rebuild-rate",
4669                                adapter->controller_proc_dir_entry);
4670                remove_proc_entry("battery-status",
4671                                adapter->controller_proc_dir_entry);
4672
4673                remove_proc_entry("diskdrives-ch0",
4674                                adapter->controller_proc_dir_entry);
4675                remove_proc_entry("diskdrives-ch1",
4676                                adapter->controller_proc_dir_entry);
4677                remove_proc_entry("diskdrives-ch2",
4678                                adapter->controller_proc_dir_entry);
4679                remove_proc_entry("diskdrives-ch3",
4680                                adapter->controller_proc_dir_entry);
4681
4682                remove_proc_entry("raiddrives-0-9",
4683                                adapter->controller_proc_dir_entry);
4684                remove_proc_entry("raiddrives-10-19",
4685                                adapter->controller_proc_dir_entry);
4686                remove_proc_entry("raiddrives-20-29",
4687                                adapter->controller_proc_dir_entry);
4688                remove_proc_entry("raiddrives-30-39",
4689                                adapter->controller_proc_dir_entry);
4690#endif
4691                {
4692                        char    buf[12] = { 0 };
4693                        sprintf(buf, "hba%d", adapter->host->host_no);
4694                        remove_proc_entry(buf, mega_proc_dir_entry);
4695                }
4696        }
4697#endif
4698
4699        pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4700                        adapter->mega_buffer, adapter->buf_dma_handle);
4701        kfree(adapter->scb_list);
4702        pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4703                        adapter->una_mbox64, adapter->una_mbox64_dma);
4704
4705        scsi_host_put(host);
4706        pci_disable_device(pdev);
4707
4708        hba_count--;
4709}
4710
4711static void
4712megaraid_shutdown(struct pci_dev *pdev)
4713{
4714        struct Scsi_Host *host = pci_get_drvdata(pdev);
4715        adapter_t *adapter = (adapter_t *)host->hostdata;
4716
4717        __megaraid_shutdown(adapter);
4718}
4719
4720static struct pci_device_id megaraid_pci_tbl[] = {
4721        {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4722                PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4723        {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4724                PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4725        {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4726                PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4727        {0,}
4728};
4729MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4730
4731static struct pci_driver megaraid_pci_driver = {
4732        .name           = "megaraid_legacy",
4733        .id_table       = megaraid_pci_tbl,
4734        .probe          = megaraid_probe_one,
4735        .remove         = megaraid_remove_one,
4736        .shutdown       = megaraid_shutdown,
4737};
4738
4739static int __init megaraid_init(void)
4740{
4741        int error;
4742
4743        if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4744                max_cmd_per_lun = MAX_CMD_PER_LUN;
4745        if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4746                max_mbox_busy_wait = MBOX_BUSY_WAIT;
4747
4748#ifdef CONFIG_PROC_FS
4749        mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
4750        if (!mega_proc_dir_entry) {
4751                printk(KERN_WARNING
4752                                "megaraid: failed to create megaraid root\n");
4753        }
4754#endif
4755        error = pci_register_driver(&megaraid_pci_driver);
4756        if (error) {
4757#ifdef CONFIG_PROC_FS
4758                remove_proc_entry("megaraid", NULL);
4759#endif
4760                return error;
4761        }
4762
4763        /*
4764         * Register the driver as a character device, for applications
4765         * to access it for ioctls.
4766         * First argument (major) to register_chrdev implies a dynamic
4767         * major number allocation.
4768         */
4769        major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4770        if (!major) {
4771                printk(KERN_WARNING
4772                                "megaraid: failed to register char device\n");
4773        }
4774
4775        return 0;
4776}
4777
4778static void __exit megaraid_exit(void)
4779{
4780        /*
4781         * Unregister the character device interface to the driver.
4782         */
4783        unregister_chrdev(major, "megadev_legacy");
4784
4785        pci_unregister_driver(&megaraid_pci_driver);
4786
4787#ifdef CONFIG_PROC_FS
4788        remove_proc_entry("megaraid", NULL);
4789#endif
4790}
4791
4792module_init(megaraid_init);
4793module_exit(megaraid_exit);
4794
4795/* vi: set ts=8 sw=8 tw=78: */
4796