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