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