3  *                      Linux MegaRAID device driver
 
   5  * Copyright (c) 2002  LSI Logic Corporation.
 
   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.
 
  12  * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
 
  14  *        - speed-ups (list handling fixes, issued_list, optimizations.)
 
  17  * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
 
  18  *        - new-style, hotplug-aware pci probing and scsi registration
 
  20  * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
 
  21  *                                              <Seokmann.Ju@lsil.com>
 
  23  * Description: Linux device driver for LSI Logic MegaRAID controller
 
  25  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
 
  28  * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
 
  29  * and others. Please send updates to the mailing list
 
  30  * linux-scsi@vger.kernel.org .
 
  36 #include <linux/blkdev.h>
 
  37 #include <asm/uaccess.h>
 
  39 #include <linux/completion.h>
 
  40 #include <linux/delay.h>
 
  41 #include <linux/proc_fs.h>
 
  42 #include <linux/reboot.h>
 
  43 #include <linux/module.h>
 
  44 #include <linux/list.h>
 
  45 #include <linux/interrupt.h>
 
  46 #include <linux/pci.h>
 
  47 #include <linux/init.h>
 
  48 #include <linux/dma-mapping.h>
 
  49 #include <linux/smp_lock.h>
 
  50 #include <scsi/scsicam.h>
 
  53 #include <scsi/scsi_host.h>
 
  57 #define MEGARAID_MODULE_VERSION "2.00.4"
 
  59 MODULE_AUTHOR ("sju@lsil.com");
 
  60 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
 
  61 MODULE_LICENSE ("GPL");
 
  62 MODULE_VERSION(MEGARAID_MODULE_VERSION);
 
  64 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
 
  65 module_param(max_cmd_per_lun, uint, 0);
 
  66 MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
 
  68 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
 
  69 module_param(max_sectors_per_io, ushort, 0);
 
  70 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
 
  73 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
 
  74 module_param(max_mbox_busy_wait, ushort, 0);
 
  75 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
 
  77 #define RDINDOOR(adapter)       readl((adapter)->mmio_base + 0x20)
 
  78 #define RDOUTDOOR(adapter)      readl((adapter)->mmio_base + 0x2C)
 
  79 #define WRINDOOR(adapter,value)  writel(value, (adapter)->mmio_base + 0x20)
 
  80 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
 
  87 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
 
  88 static struct proc_dir_entry *mega_proc_dir_entry;
 
  90 /* For controller re-ordering */
 
  91 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
 
  94  * The File Operations structure for the serial/ioctl interface of the driver
 
  96 static const struct file_operations megadev_fops = {
 
  98         .ioctl          = megadev_ioctl,
 
 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.
 
 107 static struct mcontroller mcontroller[MAX_CONTROLLERS];
 
 109 /* The current driver version */
 
 110 static u32 driver_ver = 0x02000000;
 
 112 /* major number used by the device for character interface */
 
 115 #define IS_RAID_CH(hba, ch)     (((hba)->mega_ch_class >> (ch)) & 0x01)
 
 119  * Debug variable to print some diagnostic messages
 
 121 static int trace_level;
 
 124  * mega_setup_mailbox()
 
 125  * @adapter - pointer to our soft state
 
 127  * Allocates a 8 byte aligned memory for the handshake mailbox.
 
 130 mega_setup_mailbox(adapter_t *adapter)
 
 134         adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
 
 135                         sizeof(mbox64_t), &adapter->una_mbox64_dma);
 
 137         if( !adapter->una_mbox64 ) return -1;
 
 139         adapter->mbox = &adapter->una_mbox64->mbox;
 
 141         adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
 
 144         adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
 
 146         align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
 
 148         adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
 
 151          * Register the mailbox if the controller is an io-mapped controller
 
 153         if( adapter->flag & BOARD_IOMAP ) {
 
 155                 outb(adapter->mbox_dma & 0xFF,
 
 156                                 adapter->host->io_port + MBOX_PORT0);
 
 158                 outb((adapter->mbox_dma >> 8) & 0xFF,
 
 159                                 adapter->host->io_port + MBOX_PORT1);
 
 161                 outb((adapter->mbox_dma >> 16) & 0xFF,
 
 162                                 adapter->host->io_port + MBOX_PORT2);
 
 164                 outb((adapter->mbox_dma >> 24) & 0xFF,
 
 165                                 adapter->host->io_port + MBOX_PORT3);
 
 167                 outb(ENABLE_MBOX_BYTE,
 
 168                                 adapter->host->io_port + ENABLE_MBOX_REGION);
 
 180  * mega_query_adapter()
 
 181  * @adapter - pointer to our soft state
 
 183  * Issue the adapter inquiry commands to the controller and find out
 
 184  * information and parameter about the devices attached
 
 187 mega_query_adapter(adapter_t *adapter)
 
 189         dma_addr_t      prod_info_dma_handle;
 
 190         mega_inquiry3   *inquiry3;
 
 191         u8      raw_mbox[sizeof(struct mbox_out)];
 
 195         /* Initialize adapter inquiry mailbox */
 
 197         mbox = (mbox_t *)raw_mbox;
 
 199         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
 200         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
 203          * Try to issue Inquiry3 command
 
 204          * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
 
 205          * update enquiry3 structure
 
 207         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
 209         inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
 
 211         raw_mbox[0] = FC_NEW_CONFIG;            /* i.e. mbox->cmd=0xA1 */
 
 212         raw_mbox[2] = NC_SUBOP_ENQUIRY3;        /* i.e. 0x0F */
 
 213         raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;  /* i.e. 0x02 */
 
 215         /* Issue a blocking command to the card */
 
 216         if ((retval = issue_scb_block(adapter, raw_mbox))) {
 
 217                 /* the adapter does not support 40ld */
 
 219                 mraid_ext_inquiry       *ext_inq;
 
 221                 dma_addr_t              dma_handle;
 
 223                 ext_inq = pci_alloc_consistent(adapter->dev,
 
 224                                 sizeof(mraid_ext_inquiry), &dma_handle);
 
 226                 if( ext_inq == NULL ) return -1;
 
 228                 inq = &ext_inq->raid_inq;
 
 230                 mbox->m_out.xferaddr = (u32)dma_handle;
 
 232                 /*issue old 0x04 command to adapter */
 
 233                 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
 
 235                 issue_scb_block(adapter, raw_mbox);
 
 238                  * update Enquiry3 and ProductInfo structures with
 
 239                  * mraid_inquiry structure
 
 241                 mega_8_to_40ld(inq, inquiry3,
 
 242                                 (mega_product_info *)&adapter->product_info);
 
 244                 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
 
 245                                 ext_inq, dma_handle);
 
 247         } else {                /*adapter supports 40ld */
 
 248                 adapter->flag |= BOARD_40LD;
 
 251                  * get product_info, which is static information and will be
 
 254                 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
 
 255                                 &adapter->product_info,
 
 256                                 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
 
 258                 mbox->m_out.xferaddr = prod_info_dma_handle;
 
 260                 raw_mbox[0] = FC_NEW_CONFIG;    /* i.e. mbox->cmd=0xA1 */
 
 261                 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;    /* i.e. 0x0E */
 
 263                 if ((retval = issue_scb_block(adapter, raw_mbox)))
 
 265                         "megaraid: Product_info cmd failed with error: %d\n",
 
 268                 pci_unmap_single(adapter->dev, prod_info_dma_handle,
 
 269                                 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
 
 274          * kernel scans the channels from 0 to <= max_channel
 
 276         adapter->host->max_channel =
 
 277                 adapter->product_info.nchannels + NVIRT_CHAN -1;
 
 279         adapter->host->max_id = 16;     /* max targets per channel */
 
 281         adapter->host->max_lun = 7;     /* Upto 7 luns for non disk devices */
 
 283         adapter->host->cmd_per_lun = max_cmd_per_lun;
 
 285         adapter->numldrv = inquiry3->num_ldrv;
 
 287         adapter->max_cmds = adapter->product_info.max_commands;
 
 289         if(adapter->max_cmds > MAX_COMMANDS)
 
 290                 adapter->max_cmds = MAX_COMMANDS;
 
 292         adapter->host->can_queue = adapter->max_cmds - 1;
 
 295          * Get the maximum number of scatter-gather elements supported by this
 
 298         mega_get_max_sgl(adapter);
 
 300         adapter->host->sg_tablesize = adapter->sglen;
 
 303         /* use HP firmware and bios version encoding */
 
 304         if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
 
 305                 sprintf (adapter->fw_version, "%c%d%d.%d%d",
 
 306                          adapter->product_info.fw_version[2],
 
 307                          adapter->product_info.fw_version[1] >> 8,
 
 308                          adapter->product_info.fw_version[1] & 0x0f,
 
 309                          adapter->product_info.fw_version[0] >> 8,
 
 310                          adapter->product_info.fw_version[0] & 0x0f);
 
 311                 sprintf (adapter->bios_version, "%c%d%d.%d%d",
 
 312                          adapter->product_info.bios_version[2],
 
 313                          adapter->product_info.bios_version[1] >> 8,
 
 314                          adapter->product_info.bios_version[1] & 0x0f,
 
 315                          adapter->product_info.bios_version[0] >> 8,
 
 316                          adapter->product_info.bios_version[0] & 0x0f);
 
 318                 memcpy(adapter->fw_version,
 
 319                                 (char *)adapter->product_info.fw_version, 4);
 
 320                 adapter->fw_version[4] = 0;
 
 322                 memcpy(adapter->bios_version,
 
 323                                 (char *)adapter->product_info.bios_version, 4);
 
 325                 adapter->bios_version[4] = 0;
 
 328         printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
 
 329                 adapter->fw_version, adapter->bios_version, adapter->numldrv);
 
 332          * Do we support extended (>10 bytes) cdbs
 
 334         adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
 
 335         if (adapter->support_ext_cdb)
 
 336                 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
 
 344  * @adapter - pointer to our soft state
 
 346  * Runs through the list of pending requests.
 
 349 mega_runpendq(adapter_t *adapter)
 
 351         if(!list_empty(&adapter->pending_list))
 
 352                 __mega_runpendq(adapter);
 
 357  * @scmd - Issue this scsi command
 
 358  * @done - the callback hook into the scsi mid-layer
 
 360  * The command queuing entry point for the mid-layer.
 
 363 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
 
 370         adapter = (adapter_t *)scmd->device->host->hostdata;
 
 372         scmd->scsi_done = done;
 
 376          * Allocate and build a SCB request
 
 377          * busy flag will be set if mega_build_cmd() command could not
 
 378          * allocate scb. We will return non-zero status in that case.
 
 379          * NOTE: scb can be null even though certain commands completed
 
 380          * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
 
 381          * return 0 in that case.
 
 384         spin_lock_irqsave(&adapter->lock, flags);
 
 385         scb = mega_build_cmd(adapter, scmd, &busy);
 
 389         scb->state |= SCB_PENDQ;
 
 390         list_add_tail(&scb->list, &adapter->pending_list);
 
 393          * Check if the HBA is in quiescent state, e.g., during a
 
 394          * delete logical drive opertion. If it is, don't run
 
 397         if (atomic_read(&adapter->quiescent) == 0)
 
 398                 mega_runpendq(adapter);
 
 402         spin_unlock_irqrestore(&adapter->lock, flags);
 
 407  * mega_allocate_scb()
 
 408  * @adapter - pointer to our soft state
 
 409  * @cmd - scsi command from the mid-layer
 
 411  * Allocate a SCB structure. This is the central structure for controller
 
 414 static inline scb_t *
 
 415 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
 
 417         struct list_head *head = &adapter->free_list;
 
 420         /* Unlink command from Free List */
 
 421         if( !list_empty(head) ) {
 
 423                 scb = list_entry(head->next, scb_t, list);
 
 425                 list_del_init(head->next);
 
 427                 scb->state = SCB_ACTIVE;
 
 429                 scb->dma_type = MEGA_DMA_TYPE_NONE;
 
 438  * mega_get_ldrv_num()
 
 439  * @adapter - pointer to our soft state
 
 440  * @cmd - scsi mid layer command
 
 441  * @channel - channel on the controller
 
 443  * Calculate the logical drive number based on the information in scsi command
 
 444  * and the channel number.
 
 447 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
 
 452         tgt = cmd->device->id;
 
 454         if ( tgt > adapter->this_id )
 
 455                 tgt--;  /* we do not get inquires for initiator id */
 
 457         ldrv_num = (channel * 15) + tgt;
 
 461          * If we have a logical drive with boot enabled, project it first
 
 463         if( adapter->boot_ldrv_enabled ) {
 
 464                 if( ldrv_num == 0 ) {
 
 465                         ldrv_num = adapter->boot_ldrv;
 
 468                         if( ldrv_num <= adapter->boot_ldrv ) {
 
 475          * If "delete logical drive" feature is enabled on this controller.
 
 476          * Do only if at least one delete logical drive operation was done.
 
 478          * Also, after logical drive deletion, instead of logical drive number,
 
 479          * the value returned should be 0x80+logical drive id.
 
 481          * These is valid only for IO commands.
 
 484         if (adapter->support_random_del && adapter->read_ldidmap )
 
 485                 switch (cmd->cmnd[0]) {
 
 486                 case READ_6:    /* fall through */
 
 487                 case WRITE_6:   /* fall through */
 
 488                 case READ_10:   /* fall through */
 
 498  * @adapter - pointer to our soft state
 
 499  * @cmd - Prepare using this scsi command
 
 500  * @busy - busy flag if no resources
 
 502  * Prepares a command and scatter gather list for the controller. This routine
 
 503  * also finds out if the commands is intended for a logical drive or a
 
 504  * physical device and prepares the controller command accordingly.
 
 506  * We also re-order the logical drives and physical devices based on their
 
 510 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
 
 512         mega_ext_passthru       *epthru;
 
 513         mega_passthru   *pthru;
 
 521         int     ldrv_num = 0;   /* logical drive number */
 
 525          * filter the internal and ioctl commands
 
 527         if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
 
 528                 return (scb_t *)cmd->host_scribble;
 
 531          * We know what channels our logical drives are on - mega_find_card()
 
 533         islogical = adapter->logdrv_chan[cmd->device->channel];
 
 536          * The theory: If physical drive is chosen for boot, all the physical
 
 537          * devices are exported before the logical drives, otherwise physical
 
 538          * devices are pushed after logical drives, in which case - Kernel sees
 
 539          * the physical devices on virtual channel which is obviously converted
 
 540          * to actual channel on the HBA.
 
 542         if( adapter->boot_pdrv_enabled ) {
 
 544                         /* logical channel */
 
 545                         channel = cmd->device->channel -
 
 546                                 adapter->product_info.nchannels;
 
 549                         /* this is physical channel */
 
 550                         channel = cmd->device->channel; 
 
 551                         target = cmd->device->id;
 
 554                          * boot from a physical disk, that disk needs to be
 
 555                          * exposed first IF both the channels are SCSI, then
 
 556                          * booting from the second channel is not allowed.
 
 559                                 target = adapter->boot_pdrv_tgt;
 
 561                         else if( target == adapter->boot_pdrv_tgt ) {
 
 568                         /* this is the logical channel */
 
 569                         channel = cmd->device->channel; 
 
 572                         /* physical channel */
 
 573                         channel = cmd->device->channel - NVIRT_CHAN;    
 
 574                         target = cmd->device->id;
 
 581                 /* have just LUN 0 for each target on virtual channels */
 
 582                 if (cmd->device->lun) {
 
 583                         cmd->result = (DID_BAD_TARGET << 16);
 
 588                 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
 
 591                 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
 
 592                         MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
 
 595                  * max_ldrv_num increases by 0x80 if some logical drive was
 
 598                 if(adapter->read_ldidmap)
 
 599                         max_ldrv_num += 0x80;
 
 601                 if(ldrv_num > max_ldrv_num ) {
 
 602                         cmd->result = (DID_BAD_TARGET << 16);
 
 609                 if( cmd->device->lun > 7) {
 
 611                          * Do not support lun >7 for physically accessed
 
 614                         cmd->result = (DID_BAD_TARGET << 16);
 
 622          * Logical drive commands
 
 626                 switch (cmd->cmnd[0]) {
 
 627                 case TEST_UNIT_READY:
 
 628 #if MEGA_HAVE_CLUSTERING
 
 630                          * Do we support clustering and is the support enabled
 
 631                          * If no, return success always
 
 633                         if( !adapter->has_cluster ) {
 
 634                                 cmd->result = (DID_OK << 16);
 
 639                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
 
 644                         scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
 
 645                         scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
 
 646                         scb->raw_mbox[3] = ldrv_num;
 
 648                         scb->dma_direction = PCI_DMA_NONE;
 
 652                         cmd->result = (DID_OK << 16);
 
 659                         struct scatterlist *sg;
 
 661                         sg = scsi_sglist(cmd);
 
 662                         buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
 
 664                         memset(buf, 0, cmd->cmnd[4]);
 
 665                         kunmap_atomic(buf - sg->offset, KM_IRQ0);
 
 667                         cmd->result = (DID_OK << 16);
 
 675                         if(!(adapter->flag & (1L << cmd->device->channel))) {
 
 678                                         "scsi%d: scanning scsi channel %d ",
 
 679                                                 adapter->host->host_no,
 
 680                                                 cmd->device->channel);
 
 681                                 printk("for logical drives.\n");
 
 683                                 adapter->flag |= (1L << cmd->device->channel);
 
 686                         /* Allocate a SCB and initialize passthru */
 
 687                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
 
 693                         mbox = (mbox_t *)scb->raw_mbox;
 
 694                         memset(mbox, 0, sizeof(scb->raw_mbox));
 
 695                         memset(pthru, 0, sizeof(mega_passthru));
 
 699                         pthru->reqsenselen = 14;
 
 700                         pthru->islogical = 1;
 
 701                         pthru->logdrv = ldrv_num;
 
 702                         pthru->cdblen = cmd->cmd_len;
 
 703                         memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
 
 705                         if( adapter->has_64bit_addr ) {
 
 706                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
 
 709                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
 
 712                         scb->dma_direction = PCI_DMA_FROMDEVICE;
 
 714                         pthru->numsgelements = mega_build_sglist(adapter, scb,
 
 715                                 &pthru->dataxferaddr, &pthru->dataxferlen);
 
 717                         mbox->m_out.xferaddr = scb->pthru_dma_addr;
 
 728                         /* Allocate a SCB and initialize mailbox */
 
 729                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
 
 733                         mbox = (mbox_t *)scb->raw_mbox;
 
 735                         memset(mbox, 0, sizeof(scb->raw_mbox));
 
 736                         mbox->m_out.logdrv = ldrv_num;
 
 739                          * A little hack: 2nd bit is zero for all scsi read
 
 740                          * commands and is set for all scsi write commands
 
 742                         if( adapter->has_64bit_addr ) {
 
 743                                 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
 
 744                                         MEGA_MBOXCMD_LWRITE64:
 
 745                                         MEGA_MBOXCMD_LREAD64 ;
 
 748                                 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
 
 754                          * 6-byte READ(0x08) or WRITE(0x0A) cdb
 
 756                         if( cmd->cmd_len == 6 ) {
 
 757                                 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
 
 759                                         ((u32)cmd->cmnd[1] << 16) |
 
 760                                         ((u32)cmd->cmnd[2] << 8) |
 
 763                                 mbox->m_out.lba &= 0x1FFFFF;
 
 767                                  * Take modulo 0x80, since the logical drive
 
 768                                  * number increases by 0x80 when a logical
 
 771                                 if (*cmd->cmnd == READ_6) {
 
 772                                         adapter->nreads[ldrv_num%0x80]++;
 
 773                                         adapter->nreadblocks[ldrv_num%0x80] +=
 
 774                                                 mbox->m_out.numsectors;
 
 776                                         adapter->nwrites[ldrv_num%0x80]++;
 
 777                                         adapter->nwriteblocks[ldrv_num%0x80] +=
 
 778                                                 mbox->m_out.numsectors;
 
 784                          * 10-byte READ(0x28) or WRITE(0x2A) cdb
 
 786                         if( cmd->cmd_len == 10 ) {
 
 787                                 mbox->m_out.numsectors =
 
 789                                         ((u32)cmd->cmnd[7] << 8);
 
 791                                         ((u32)cmd->cmnd[2] << 24) |
 
 792                                         ((u32)cmd->cmnd[3] << 16) |
 
 793                                         ((u32)cmd->cmnd[4] << 8) |
 
 797                                 if (*cmd->cmnd == READ_10) {
 
 798                                         adapter->nreads[ldrv_num%0x80]++;
 
 799                                         adapter->nreadblocks[ldrv_num%0x80] +=
 
 800                                                 mbox->m_out.numsectors;
 
 802                                         adapter->nwrites[ldrv_num%0x80]++;
 
 803                                         adapter->nwriteblocks[ldrv_num%0x80] +=
 
 804                                                 mbox->m_out.numsectors;
 
 810                          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
 
 812                         if( cmd->cmd_len == 12 ) {
 
 814                                         ((u32)cmd->cmnd[2] << 24) |
 
 815                                         ((u32)cmd->cmnd[3] << 16) |
 
 816                                         ((u32)cmd->cmnd[4] << 8) |
 
 819                                 mbox->m_out.numsectors =
 
 820                                         ((u32)cmd->cmnd[6] << 24) |
 
 821                                         ((u32)cmd->cmnd[7] << 16) |
 
 822                                         ((u32)cmd->cmnd[8] << 8) |
 
 826                                 if (*cmd->cmnd == READ_12) {
 
 827                                         adapter->nreads[ldrv_num%0x80]++;
 
 828                                         adapter->nreadblocks[ldrv_num%0x80] +=
 
 829                                                 mbox->m_out.numsectors;
 
 831                                         adapter->nwrites[ldrv_num%0x80]++;
 
 832                                         adapter->nwriteblocks[ldrv_num%0x80] +=
 
 833                                                 mbox->m_out.numsectors;
 
 839                          * If it is a read command
 
 841                         if( (*cmd->cmnd & 0x0F) == 0x08 ) {
 
 842                                 scb->dma_direction = PCI_DMA_FROMDEVICE;
 
 845                                 scb->dma_direction = PCI_DMA_TODEVICE;
 
 848                         /* Calculate Scatter-Gather info */
 
 849                         mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
 
 850                                         (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
 
 854 #if MEGA_HAVE_CLUSTERING
 
 855                 case RESERVE:   /* Fall through */
 
 859                          * Do we support clustering and is the support enabled
 
 861                         if( ! adapter->has_cluster ) {
 
 863                                 cmd->result = (DID_BAD_TARGET << 16);
 
 868                         /* Allocate a SCB and initialize mailbox */
 
 869                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
 
 874                         scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
 
 875                         scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
 
 876                                 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
 
 878                         scb->raw_mbox[3] = ldrv_num;
 
 880                         scb->dma_direction = PCI_DMA_NONE;
 
 886                         cmd->result = (DID_BAD_TARGET << 16);
 
 893          * Passthru drive commands
 
 896                 /* Allocate a SCB and initialize passthru */
 
 897                 if(!(scb = mega_allocate_scb(adapter, cmd))) {
 
 902                 mbox = (mbox_t *)scb->raw_mbox;
 
 903                 memset(mbox, 0, sizeof(scb->raw_mbox));
 
 905                 if( adapter->support_ext_cdb ) {
 
 907                         epthru = mega_prepare_extpassthru(adapter, scb, cmd,
 
 910                         mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
 
 912                         mbox->m_out.xferaddr = scb->epthru_dma_addr;
 
 917                         pthru = mega_prepare_passthru(adapter, scb, cmd,
 
 920                         /* Initialize mailbox */
 
 921                         if( adapter->has_64bit_addr ) {
 
 922                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
 
 925                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
 
 928                         mbox->m_out.xferaddr = scb->pthru_dma_addr;
 
 938  * mega_prepare_passthru()
 
 939  * @adapter - pointer to our soft state
 
 940  * @scb - our scsi control block
 
 941  * @cmd - scsi command from the mid-layer
 
 942  * @channel - actual channel on the controller
 
 943  * @target - actual id on the controller.
 
 945  * prepare a command for the scsi physical devices.
 
 947 static mega_passthru *
 
 948 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
 
 949                 int channel, int target)
 
 951         mega_passthru *pthru;
 
 954         memset(pthru, 0, sizeof (mega_passthru));
 
 956         /* 0=6sec/1=60sec/2=10min/3=3hrs */
 
 960         pthru->reqsenselen = 14;
 
 961         pthru->islogical = 0;
 
 963         pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
 
 965         pthru->target = (adapter->flag & BOARD_40LD) ?
 
 966                 (channel << 4) | target : target;
 
 968         pthru->cdblen = cmd->cmd_len;
 
 969         pthru->logdrv = cmd->device->lun;
 
 971         memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
 
 973         /* Not sure about the direction */
 
 974         scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
 
 976         /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
 
 977         switch (cmd->cmnd[0]) {
 
 980                 if(!(adapter->flag & (1L << cmd->device->channel))) {
 
 983                                 "scsi%d: scanning scsi channel %d [P%d] ",
 
 984                                         adapter->host->host_no,
 
 985                                         cmd->device->channel, channel);
 
 986                         printk("for physical devices.\n");
 
 988                         adapter->flag |= (1L << cmd->device->channel);
 
 992                 pthru->numsgelements = mega_build_sglist(adapter, scb,
 
 993                                 &pthru->dataxferaddr, &pthru->dataxferlen);
 
1001  * mega_prepare_extpassthru()
 
1002  * @adapter - pointer to our soft state
 
1003  * @scb - our scsi control block
 
1004  * @cmd - scsi command from the mid-layer
 
1005  * @channel - actual channel on the controller
 
1006  * @target - actual id on the controller.
 
1008  * prepare a command for the scsi physical devices. This rountine prepares
 
1009  * commands for devices which can take extended CDBs (>10 bytes)
 
1011 static mega_ext_passthru *
 
1012 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
 
1013                 int channel, int target)
 
1015         mega_ext_passthru       *epthru;
 
1017         epthru = scb->epthru;
 
1018         memset(epthru, 0, sizeof(mega_ext_passthru));
 
1020         /* 0=6sec/1=60sec/2=10min/3=3hrs */
 
1021         epthru->timeout = 2;
 
1024         epthru->reqsenselen = 14;
 
1025         epthru->islogical = 0;
 
1027         epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
 
1028         epthru->target = (adapter->flag & BOARD_40LD) ?
 
1029                 (channel << 4) | target : target;
 
1031         epthru->cdblen = cmd->cmd_len;
 
1032         epthru->logdrv = cmd->device->lun;
 
1034         memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
 
1036         /* Not sure about the direction */
 
1037         scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
 
1039         switch(cmd->cmnd[0]) {
 
1042                 if(!(adapter->flag & (1L << cmd->device->channel))) {
 
1045                                 "scsi%d: scanning scsi channel %d [P%d] ",
 
1046                                         adapter->host->host_no,
 
1047                                         cmd->device->channel, channel);
 
1048                         printk("for physical devices.\n");
 
1050                         adapter->flag |= (1L << cmd->device->channel);
 
1054                 epthru->numsgelements = mega_build_sglist(adapter, scb,
 
1055                                 &epthru->dataxferaddr, &epthru->dataxferlen);
 
1063 __mega_runpendq(adapter_t *adapter)
 
1066         struct list_head *pos, *next;
 
1068         /* Issue any pending commands to the card */
 
1069         list_for_each_safe(pos, next, &adapter->pending_list) {
 
1071                 scb = list_entry(pos, scb_t, list);
 
1073                 if( !(scb->state & SCB_ISSUED) ) {
 
1075                         if( issue_scb(adapter, scb) != 0 )
 
1086  * @adapter - pointer to our soft state
 
1087  * @scb - scsi control block
 
1089  * Post a command to the card if the mailbox is available, otherwise return
 
1090  * busy. We also take the scb from the pending list if the mailbox is
 
1094 issue_scb(adapter_t *adapter, scb_t *scb)
 
1096         volatile mbox64_t       *mbox64 = adapter->mbox64;
 
1097         volatile mbox_t         *mbox = adapter->mbox;
 
1100         if(unlikely(mbox->m_in.busy)) {
 
1104                 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
 
1106                 if(mbox->m_in.busy) return -1;
 
1109         /* Copy mailbox data into host structure */
 
1110         memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox, 
 
1111                         sizeof(struct mbox_out));
 
1113         mbox->m_out.cmdid = scb->idx;   /* Set cmdid */
 
1114         mbox->m_in.busy = 1;            /* Set busy */
 
1118          * Increment the pending queue counter
 
1120         atomic_inc(&adapter->pend_cmds);
 
1122         switch (mbox->m_out.cmd) {
 
1123         case MEGA_MBOXCMD_LREAD64:
 
1124         case MEGA_MBOXCMD_LWRITE64:
 
1125         case MEGA_MBOXCMD_PASSTHRU64:
 
1126         case MEGA_MBOXCMD_EXTPTHRU:
 
1127                 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
 
1128                 mbox64->xfer_segment_hi = 0;
 
1129                 mbox->m_out.xferaddr = 0xFFFFFFFF;
 
1132                 mbox64->xfer_segment_lo = 0;
 
1133                 mbox64->xfer_segment_hi = 0;
 
1139         scb->state |= SCB_ISSUED;
 
1141         if( likely(adapter->flag & BOARD_MEMMAP) ) {
 
1142                 mbox->m_in.poll = 0;
 
1144                 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
 
1147                 irq_enable(adapter);
 
1148                 issue_command(adapter);
 
1155  * Wait until the controller's mailbox is available
 
1158 mega_busywait_mbox (adapter_t *adapter)
 
1160         if (adapter->mbox->m_in.busy)
 
1161                 return __mega_busywait_mbox(adapter);
 
1167  * @adapter - pointer to our soft state
 
1168  * @raw_mbox - the mailbox
 
1170  * Issue a scb in synchronous and non-interrupt mode
 
1173 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
 
1175         volatile mbox64_t *mbox64 = adapter->mbox64;
 
1176         volatile mbox_t *mbox = adapter->mbox;
 
1179         /* Wait until mailbox is free */
 
1180         if(mega_busywait_mbox (adapter))
 
1181                 goto bug_blocked_mailbox;
 
1183         /* Copy mailbox data into host structure */
 
1184         memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
 
1185         mbox->m_out.cmdid = 0xFE;
 
1186         mbox->m_in.busy = 1;
 
1188         switch (raw_mbox[0]) {
 
1189         case MEGA_MBOXCMD_LREAD64:
 
1190         case MEGA_MBOXCMD_LWRITE64:
 
1191         case MEGA_MBOXCMD_PASSTHRU64:
 
1192         case MEGA_MBOXCMD_EXTPTHRU:
 
1193                 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
 
1194                 mbox64->xfer_segment_hi = 0;
 
1195                 mbox->m_out.xferaddr = 0xFFFFFFFF;
 
1198                 mbox64->xfer_segment_lo = 0;
 
1199                 mbox64->xfer_segment_hi = 0;
 
1202         if( likely(adapter->flag & BOARD_MEMMAP) ) {
 
1203                 mbox->m_in.poll = 0;
 
1205                 mbox->m_in.numstatus = 0xFF;
 
1206                 mbox->m_in.status = 0xFF;
 
1207                 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
 
1209                 while((volatile u8)mbox->m_in.numstatus == 0xFF)
 
1212                 mbox->m_in.numstatus = 0xFF;
 
1214                 while( (volatile u8)mbox->m_in.poll != 0x77 )
 
1217                 mbox->m_in.poll = 0;
 
1218                 mbox->m_in.ack = 0x77;
 
1220                 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
 
1222                 while(RDINDOOR(adapter) & 0x2)
 
1226                 irq_disable(adapter);
 
1227                 issue_command(adapter);
 
1229                 while (!((byte = irq_state(adapter)) & INTR_VALID))
 
1232                 set_irq_state(adapter, byte);
 
1233                 irq_enable(adapter);
 
1237         return mbox->m_in.status;
 
1239 bug_blocked_mailbox:
 
1240         printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
 
1247  * megaraid_isr_iomapped()
 
1249  * @devp - pointer to our soft state
 
1251  * Interrupt service routine for io-mapped controllers.
 
1252  * Find out if our device is interrupting. If yes, acknowledge the interrupt
 
1253  * and service the completed commands.
 
1256 megaraid_isr_iomapped(int irq, void *devp)
 
1258         adapter_t       *adapter = devp;
 
1259         unsigned long   flags;
 
1262         u8      completed[MAX_FIRMWARE_STATUS];
 
1268          * loop till F/W has more commands for us to complete.
 
1270         spin_lock_irqsave(&adapter->lock, flags);
 
1273                 /* Check if a valid interrupt is pending */
 
1274                 byte = irq_state(adapter);
 
1275                 if( (byte & VALID_INTR_BYTE) == 0 ) {
 
1277                          * No more pending commands
 
1281                 set_irq_state(adapter, byte);
 
1283                 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
 
1286                 adapter->mbox->m_in.numstatus = 0xFF;
 
1288                 status = adapter->mbox->m_in.status;
 
1291                  * decrement the pending queue counter
 
1293                 atomic_sub(nstatus, &adapter->pend_cmds);
 
1295                 memcpy(completed, (void *)adapter->mbox->m_in.completed, 
 
1298                 /* Acknowledge interrupt */
 
1301                 mega_cmd_done(adapter, completed, nstatus, status);
 
1303                 mega_rundoneq(adapter);
 
1307                 /* Loop through any pending requests */
 
1308                 if(atomic_read(&adapter->quiescent) == 0) {
 
1309                         mega_runpendq(adapter);
 
1316         spin_unlock_irqrestore(&adapter->lock, flags);
 
1318         return IRQ_RETVAL(handled);
 
1323  * megaraid_isr_memmapped()
 
1325  * @devp - pointer to our soft state
 
1327  * Interrupt service routine for memory-mapped controllers.
 
1328  * Find out if our device is interrupting. If yes, acknowledge the interrupt
 
1329  * and service the completed commands.
 
1332 megaraid_isr_memmapped(int irq, void *devp)
 
1334         adapter_t       *adapter = devp;
 
1335         unsigned long   flags;
 
1339         u8      completed[MAX_FIRMWARE_STATUS];
 
1344          * loop till F/W has more commands for us to complete.
 
1346         spin_lock_irqsave(&adapter->lock, flags);
 
1349                 /* Check if a valid interrupt is pending */
 
1350                 dword = RDOUTDOOR(adapter);
 
1351                 if(dword != 0x10001234) {
 
1353                          * No more pending commands
 
1357                 WROUTDOOR(adapter, 0x10001234);
 
1359                 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
 
1363                 adapter->mbox->m_in.numstatus = 0xFF;
 
1365                 status = adapter->mbox->m_in.status;
 
1368                  * decrement the pending queue counter
 
1370                 atomic_sub(nstatus, &adapter->pend_cmds);
 
1372                 memcpy(completed, (void *)adapter->mbox->m_in.completed, 
 
1375                 /* Acknowledge interrupt */
 
1376                 WRINDOOR(adapter, 0x2);
 
1380                 while( RDINDOOR(adapter) & 0x02 )
 
1383                 mega_cmd_done(adapter, completed, nstatus, status);
 
1385                 mega_rundoneq(adapter);
 
1387                 /* Loop through any pending requests */
 
1388                 if(atomic_read(&adapter->quiescent) == 0) {
 
1389                         mega_runpendq(adapter);
 
1396         spin_unlock_irqrestore(&adapter->lock, flags);
 
1398         return IRQ_RETVAL(handled);
 
1402  * @adapter - pointer to our soft state
 
1403  * @completed - array of ids of completed commands
 
1404  * @nstatus - number of completed commands
 
1405  * @status - status of the last command completed
 
1407  * Complete the comamnds and call the scsi mid-layer callback hooks.
 
1410 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
 
1412         mega_ext_passthru       *epthru = NULL;
 
1413         struct scatterlist      *sgl;
 
1414         Scsi_Cmnd       *cmd = NULL;
 
1415         mega_passthru   *pthru = NULL;
 
1416         mbox_t  *mbox = NULL;
 
1424          * for all the commands completed, call the mid-layer callback routine
 
1427         for( i = 0; i < nstatus; i++ ) {
 
1429                 cmdid = completed[i];
 
1431                 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
 
1432                         scb = &adapter->int_scb;
 
1434                         mbox = (mbox_t *)scb->raw_mbox;
 
1437                          * Internal command interface do not fire the extended
 
1438                          * passthru or 64-bit passthru
 
1444                         scb = &adapter->scb_list[cmdid];
 
1447                          * Make sure f/w has completed a valid command
 
1449                         if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
 
1451                                         "megaraid: invalid command ");
 
1452                                 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
 
1453                                         cmdid, scb->state, scb->cmd);
 
1459                          * Was a abort issued for this command
 
1461                         if( scb->state & SCB_ABORT ) {
 
1464                                 "megaraid: aborted cmd %lx[%x] complete.\n",
 
1465                                         scb->cmd->serial_number, scb->idx);
 
1467                                 scb->cmd->result = (DID_ABORT << 16);
 
1469                                 list_add_tail(SCSI_LIST(scb->cmd),
 
1470                                                 &adapter->completed_list);
 
1472                                 mega_free_scb(adapter, scb);
 
1478                          * Was a reset issued for this command
 
1480                         if( scb->state & SCB_RESET ) {
 
1483                                 "megaraid: reset cmd %lx[%x] complete.\n",
 
1484                                         scb->cmd->serial_number, scb->idx);
 
1486                                 scb->cmd->result = (DID_RESET << 16);
 
1488                                 list_add_tail(SCSI_LIST(scb->cmd),
 
1489                                                 &adapter->completed_list);
 
1491                                 mega_free_scb (adapter, scb);
 
1498                         epthru = scb->epthru;
 
1499                         mbox = (mbox_t *)scb->raw_mbox;
 
1504                         int     logdrv = mbox->m_out.logdrv;
 
1506                         islogical = adapter->logdrv_chan[cmd->channel];
 
1508                          * Maintain an error counter for the logical drive.
 
1509                          * Some application like SNMP agent need such
 
1512                         if( status && islogical && (cmd->cmnd[0] == READ_6 ||
 
1513                                                 cmd->cmnd[0] == READ_10 ||
 
1514                                                 cmd->cmnd[0] == READ_12)) {
 
1516                                  * Logical drive number increases by 0x80 when
 
1517                                  * a logical drive is deleted
 
1519                                 adapter->rd_errors[logdrv%0x80]++;
 
1522                         if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
 
1523                                                 cmd->cmnd[0] == WRITE_10 ||
 
1524                                                 cmd->cmnd[0] == WRITE_12)) {
 
1526                                  * Logical drive number increases by 0x80 when
 
1527                                  * a logical drive is deleted
 
1529                                 adapter->wr_errors[logdrv%0x80]++;
 
1537                  * Do not return the presence of hard disk on the channel so,
 
1538                  * inquiry sent, and returned data==hard disk or removable
 
1539                  * hard disk and not logical, request should return failure! -
 
1542                 islogical = adapter->logdrv_chan[cmd->device->channel];
 
1543                 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
 
1545                         sgl = scsi_sglist(cmd);
 
1546                         if( sg_page(sgl) ) {
 
1547                                 c = *(unsigned char *) sg_virt(&sgl[0]);
 
1550                                        "megaraid: invalid sg.\n");
 
1554                         if(IS_RAID_CH(adapter, cmd->device->channel) &&
 
1555                                         ((c & 0x1F ) == TYPE_DISK)) {
 
1560                 /* clear result; otherwise, success returns corrupt value */
 
1563                 /* Convert MegaRAID status to Linux error code */
 
1565                 case 0x00:      /* SUCCESS , i.e. SCSI_STATUS_GOOD */
 
1566                         cmd->result |= (DID_OK << 16);
 
1569                 case 0x02:      /* ERROR_ABORTED, i.e.
 
1570                                    SCSI_STATUS_CHECK_CONDITION */
 
1572                         /* set sense_buffer and result fields */
 
1573                         if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
 
1574                                 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
 
1576                                 memcpy(cmd->sense_buffer, pthru->reqsensearea,
 
1579                                 cmd->result = (DRIVER_SENSE << 24) |
 
1581                                         (CHECK_CONDITION << 1);
 
1584                                 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
 
1586                                         memcpy(cmd->sense_buffer,
 
1587                                                 epthru->reqsensearea, 14);
 
1589                                         cmd->result = (DRIVER_SENSE << 24) |
 
1591                                                 (CHECK_CONDITION << 1);
 
1593                                         cmd->sense_buffer[0] = 0x70;
 
1594                                         cmd->sense_buffer[2] = ABORTED_COMMAND;
 
1595                                         cmd->result |= (CHECK_CONDITION << 1);
 
1600                 case 0x08:      /* ERR_DEST_DRIVE_FAILED, i.e.
 
1602                         cmd->result |= (DID_BUS_BUSY << 16) | status;
 
1606 #if MEGA_HAVE_CLUSTERING
 
1608                          * If TEST_UNIT_READY fails, we know
 
1609                          * MEGA_RESERVATION_STATUS failed
 
1611                         if( cmd->cmnd[0] == TEST_UNIT_READY ) {
 
1612                                 cmd->result |= (DID_ERROR << 16) |
 
1613                                         (RESERVATION_CONFLICT << 1);
 
1617                          * Error code returned is 1 if Reserve or Release
 
1618                          * failed or the input parameter is invalid
 
1621                                 (cmd->cmnd[0] == RESERVE ||
 
1622                                          cmd->cmnd[0] == RELEASE) ) {
 
1624                                 cmd->result |= (DID_ERROR << 16) |
 
1625                                         (RESERVATION_CONFLICT << 1);
 
1629                                 cmd->result |= (DID_BAD_TARGET << 16)|status;
 
1633                  * Only free SCBs for the commands coming down from the
 
1634                  * mid-layer, not for which were issued internally
 
1636                  * For internal command, restore the status returned by the
 
1637                  * firmware so that user can interpret it.
 
1639                 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
 
1640                         cmd->result = status;
 
1643                          * Remove the internal command from the pending list
 
1645                         list_del_init(&scb->list);
 
1646                         scb->state = SCB_FREE;
 
1649                         mega_free_scb(adapter, scb);
 
1652                 /* Add Scsi_Command to end of completed queue */
 
1653                 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
 
1661  * Run through the list of completed requests and finish it
 
1664 mega_rundoneq (adapter_t *adapter)
 
1667         struct list_head *pos;
 
1669         list_for_each(pos, &adapter->completed_list) {
 
1671                 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
 
1673                 cmd = list_entry(spos, Scsi_Cmnd, SCp);
 
1674                 cmd->scsi_done(cmd);
 
1677         INIT_LIST_HEAD(&adapter->completed_list);
 
1682  * Free a SCB structure
 
1683  * Note: We assume the scsi commands associated with this scb is not free yet.
 
1686 mega_free_scb(adapter_t *adapter, scb_t *scb)
 
1688         switch( scb->dma_type ) {
 
1690         case MEGA_DMA_TYPE_NONE:
 
1694                 scsi_dma_unmap(scb->cmd);
 
1701          * Remove from the pending list
 
1703         list_del_init(&scb->list);
 
1705         /* Link the scb back into free list */
 
1706         scb->state = SCB_FREE;
 
1709         list_add(&scb->list, &adapter->free_list);
 
1714 __mega_busywait_mbox (adapter_t *adapter)
 
1716         volatile mbox_t *mbox = adapter->mbox;
 
1719         for (counter = 0; counter < 10000; counter++) {
 
1720                 if (!mbox->m_in.busy)
 
1725         return -1;              /* give up after 1 second */
 
1729  * Copies data to SGLIST
 
1730  * Note: For 64 bit cards, we need a minimum of one SG element for read/write
 
1733 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
 
1735         struct scatterlist *sg;
 
1743          * Copy Scatter-Gather list info into controller structure.
 
1745          * The number of sg elements returned must not exceed our limit
 
1747         sgcnt = scsi_dma_map(cmd);
 
1749         scb->dma_type = MEGA_SGLIST;
 
1751         BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
 
1755         if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
 
1756                 sg = scsi_sglist(cmd);
 
1757                 scb->dma_h_bulkdata = sg_dma_address(sg);
 
1758                 *buf = (u32)scb->dma_h_bulkdata;
 
1759                 *len = sg_dma_len(sg);
 
1763         scsi_for_each_sg(cmd, sg, sgcnt, idx) {
 
1764                 if (adapter->has_64bit_addr) {
 
1765                         scb->sgl64[idx].address = sg_dma_address(sg);
 
1766                         *len += scb->sgl64[idx].length = sg_dma_len(sg);
 
1768                         scb->sgl[idx].address = sg_dma_address(sg);
 
1769                         *len += scb->sgl[idx].length = sg_dma_len(sg);
 
1773         /* Reset pointer and length fields */
 
1774         *buf = scb->sgl_dma_addr;
 
1776         /* Return count of SG requests */
 
1784  * takes all info in AdapterInquiry structure and puts it into ProductInfo and
 
1785  * Enquiry3 structures for later use
 
1788 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
 
1789                 mega_product_info *product_info)
 
1793         product_info->max_commands = inquiry->adapter_info.max_commands;
 
1794         enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
 
1795         product_info->nchannels = inquiry->adapter_info.nchannels;
 
1797         for (i = 0; i < 4; i++) {
 
1798                 product_info->fw_version[i] =
 
1799                         inquiry->adapter_info.fw_version[i];
 
1801                 product_info->bios_version[i] =
 
1802                         inquiry->adapter_info.bios_version[i];
 
1804         enquiry3->cache_flush_interval =
 
1805                 inquiry->adapter_info.cache_flush_interval;
 
1807         product_info->dram_size = inquiry->adapter_info.dram_size;
 
1809         enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
 
1811         for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
 
1812                 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
 
1813                 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
 
1814                 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
 
1817         for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
 
1818                 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
 
1822 mega_free_sgl(adapter_t *adapter)
 
1827         for(i = 0; i < adapter->max_cmds; i++) {
 
1829                 scb = &adapter->scb_list[i];
 
1832                         pci_free_consistent(adapter->dev,
 
1833                                 sizeof(mega_sgl64) * adapter->sglen,
 
1841                         pci_free_consistent(adapter->dev, sizeof(mega_passthru),
 
1842                                 scb->pthru, scb->pthru_dma_addr);
 
1848                         pci_free_consistent(adapter->dev,
 
1849                                 sizeof(mega_ext_passthru),
 
1850                                 scb->epthru, scb->epthru_dma_addr);
 
1860  * Get information about the card/driver
 
1863 megaraid_info(struct Scsi_Host *host)
 
1865         static char buffer[512];
 
1868         adapter = (adapter_t *)host->hostdata;
 
1871                  "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
 
1872                  adapter->fw_version, adapter->product_info.max_commands,
 
1873                  adapter->host->max_id, adapter->host->max_channel,
 
1874                  adapter->host->max_lun);
 
1879  * Abort a previous SCSI request. Only commands on the pending list can be
 
1880  * aborted. All the commands issued to the F/W must complete.
 
1883 megaraid_abort(Scsi_Cmnd *cmd)
 
1888         adapter = (adapter_t *)cmd->device->host->hostdata;
 
1890         rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
 
1893          * This is required here to complete any completed requests
 
1894          * to be communicated over to the mid layer.
 
1896         mega_rundoneq(adapter);
 
1903 megaraid_reset(struct scsi_cmnd *cmd)
 
1909         adapter = (adapter_t *)cmd->device->host->hostdata;
 
1911 #if MEGA_HAVE_CLUSTERING
 
1912         mc.cmd = MEGA_CLUSTER_CMD;
 
1913         mc.opcode = MEGA_RESET_RESERVATIONS;
 
1915         if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
 
1917                                 "megaraid: reservation reset failed.\n");
 
1920                 printk(KERN_INFO "megaraid: reservation reset.\n");
 
1924         spin_lock_irq(&adapter->lock);
 
1926         rval =  megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
 
1929          * This is required here to complete any completed requests
 
1930          * to be communicated over to the mid layer.
 
1932         mega_rundoneq(adapter);
 
1933         spin_unlock_irq(&adapter->lock);
 
1939  * megaraid_abort_and_reset()
 
1940  * @adapter - megaraid soft state
 
1941  * @cmd - scsi command to be aborted or reset
 
1942  * @aor - abort or reset flag
 
1944  * Try to locate the scsi command in the pending queue. If found and is not
 
1945  * issued to the controller, abort/reset it. Otherwise return failure
 
1948 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
 
1950         struct list_head        *pos, *next;
 
1953         printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
 
1954              (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
 
1955              cmd->cmnd[0], cmd->device->channel, 
 
1956              cmd->device->id, cmd->device->lun);
 
1958         if(list_empty(&adapter->pending_list))
 
1961         list_for_each_safe(pos, next, &adapter->pending_list) {
 
1963                 scb = list_entry(pos, scb_t, list);
 
1965                 if (scb->cmd == cmd) { /* Found command */
 
1970                          * Check if this command has firmare owenership. If
 
1971                          * yes, we cannot reset this command. Whenever, f/w
 
1972                          * completes this command, we will return appropriate
 
1975                         if( scb->state & SCB_ISSUED ) {
 
1978                                         "megaraid: %s-%lx[%x], fw owner.\n",
 
1979                                         (aor==SCB_ABORT) ? "ABORTING":"RESET",
 
1980                                         cmd->serial_number, scb->idx);
 
1987                                  * Not yet issued! Remove from the pending
 
1991                                         "megaraid: %s-%lx[%x], driver owner.\n",
 
1992                                         (aor==SCB_ABORT) ? "ABORTING":"RESET",
 
1993                                         cmd->serial_number, scb->idx);
 
1995                                 mega_free_scb(adapter, scb);
 
1997                                 if( aor == SCB_ABORT ) {
 
1998                                         cmd->result = (DID_ABORT << 16);
 
2001                                         cmd->result = (DID_RESET << 16);
 
2004                                 list_add_tail(SCSI_LIST(cmd),
 
2005                                                 &adapter->completed_list);
 
2016 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
 
2018         *pdev = alloc_pci_dev();
 
2020         if( *pdev == NULL ) return -1;
 
2022         memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
 
2024         if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
 
2033 free_local_pdev(struct pci_dev *pdev)
 
2039  * mega_allocate_inquiry()
 
2040  * @dma_handle - handle returned for dma address
 
2041  * @pdev - handle to pci device
 
2043  * allocates memory for inquiry structure
 
2045 static inline void *
 
2046 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
 
2048         return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
 
2053 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
 
2055         pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
 
2059 #ifdef CONFIG_PROC_FS
 
2060 /* Following code handles /proc fs  */
 
2062 #define CREATE_READ_PROC(string, func)  create_proc_read_entry(string,  \
 
2063                                         S_IRUSR | S_IFREG,              \
 
2064                                         controller_proc_dir_entry,      \
 
2068  * mega_create_proc_entry()
 
2069  * @index - index in soft state array
 
2070  * @parent - parent node for this /proc entry
 
2072  * Creates /proc entries for our controllers.
 
2075 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
 
2077         struct proc_dir_entry   *controller_proc_dir_entry = NULL;
 
2078         u8              string[64] = { 0 };
 
2079         adapter_t       *adapter = hba_soft_state[index];
 
2081         sprintf(string, "hba%d", adapter->host->host_no);
 
2083         controller_proc_dir_entry =
 
2084                 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
 
2086         if(!controller_proc_dir_entry) {
 
2087                 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
 
2090         adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
 
2091         adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
 
2092         adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
 
2093 #if MEGA_HAVE_ENH_PROC
 
2094         adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
 
2095         adapter->proc_battery = CREATE_READ_PROC("battery-status",
 
2099          * Display each physical drive on its channel
 
2101         adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
 
2103         adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
 
2105         adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
 
2107         adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
 
2111          * Display a set of up to 10 logical drive through each of following
 
2114         adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
 
2116         adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
 
2118         adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
 
2120         adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
 
2127  * proc_read_config()
 
2128  * @page - buffer to write the data in
 
2129  * @start - where the actual data has been written in page
 
2130  * @offset - same meaning as the read system call
 
2131  * @count - same meaning as the read system call
 
2132  * @eof - set if no more data needs to be returned
 
2133  * @data - pointer to our soft state
 
2135  * Display configuration information about the controller.
 
2138 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
 
2142         adapter_t *adapter = (adapter_t *)data;
 
2145         len += sprintf(page+len, "%s", MEGARAID_VERSION);
 
2147         if(adapter->product_info.product_name[0])
 
2148                 len += sprintf(page+len, "%s\n",
 
2149                                 adapter->product_info.product_name);
 
2151         len += sprintf(page+len, "Controller Type: ");
 
2153         if( adapter->flag & BOARD_MEMMAP ) {
 
2154                 len += sprintf(page+len,
 
2155                         "438/466/467/471/493/518/520/531/532\n");
 
2158                 len += sprintf(page+len,
 
2162         if(adapter->flag & BOARD_40LD) {
 
2163                 len += sprintf(page+len,
 
2164                                 "Controller Supports 40 Logical Drives\n");
 
2167         if(adapter->flag & BOARD_64BIT) {
 
2168                 len += sprintf(page+len,
 
2169                 "Controller capable of 64-bit memory addressing\n");
 
2171         if( adapter->has_64bit_addr ) {
 
2172                 len += sprintf(page+len,
 
2173                         "Controller using 64-bit memory addressing\n");
 
2176                 len += sprintf(page+len,
 
2177                         "Controller is not using 64-bit memory addressing\n");
 
2180         len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
 
2181                         adapter->host->irq);
 
2183         len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
 
2184                         adapter->numldrv, adapter->product_info.nchannels);
 
2186         len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
 
2187                         adapter->fw_version, adapter->bios_version,
 
2188                         adapter->product_info.dram_size);
 
2190         len += sprintf(page+len,
 
2191                 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
 
2192                 adapter->product_info.max_commands, adapter->max_cmds);
 
2194         len += sprintf(page+len, "support_ext_cdb    = %d\n",
 
2195                         adapter->support_ext_cdb);
 
2196         len += sprintf(page+len, "support_random_del = %d\n",
 
2197                         adapter->support_random_del);
 
2198         len += sprintf(page+len, "boot_ldrv_enabled  = %d\n",
 
2199                         adapter->boot_ldrv_enabled);
 
2200         len += sprintf(page+len, "boot_ldrv          = %d\n",
 
2201                         adapter->boot_ldrv);
 
2202         len += sprintf(page+len, "boot_pdrv_enabled  = %d\n",
 
2203                         adapter->boot_pdrv_enabled);
 
2204         len += sprintf(page+len, "boot_pdrv_ch       = %d\n",
 
2205                         adapter->boot_pdrv_ch);
 
2206         len += sprintf(page+len, "boot_pdrv_tgt      = %d\n",
 
2207                         adapter->boot_pdrv_tgt);
 
2208         len += sprintf(page+len, "quiescent          = %d\n",
 
2209                         atomic_read(&adapter->quiescent));
 
2210         len += sprintf(page+len, "has_cluster        = %d\n",
 
2211                         adapter->has_cluster);
 
2213         len += sprintf(page+len, "\nModule Parameters:\n");
 
2214         len += sprintf(page+len, "max_cmd_per_lun    = %d\n",
 
2216         len += sprintf(page+len, "max_sectors_per_io = %d\n",
 
2217                         max_sectors_per_io);
 
2228  * @page - buffer to write the data in
 
2229  * @start - where the actual data has been written in page
 
2230  * @offset - same meaning as the read system call
 
2231  * @count - same meaning as the read system call
 
2232  * @eof - set if no more data needs to be returned
 
2233  * @data - pointer to our soft state
 
2235  * Diaplay statistical information about the I/O activity.
 
2238 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
 
2245         i = 0;  /* avoid compilation warnings */
 
2247         adapter = (adapter_t *)data;
 
2249         len = sprintf(page, "Statistical Information for this controller\n");
 
2250         len += sprintf(page+len, "pend_cmds = %d\n",
 
2251                         atomic_read(&adapter->pend_cmds));
 
2253         for(i = 0; i < adapter->numldrv; i++) {
 
2254                 len += sprintf(page+len, "Logical Drive %d:\n", i);
 
2256                 len += sprintf(page+len,
 
2257                         "\tReads Issued = %lu, Writes Issued = %lu\n",
 
2258                         adapter->nreads[i], adapter->nwrites[i]);
 
2260                 len += sprintf(page+len,
 
2261                         "\tSectors Read = %lu, Sectors Written = %lu\n",
 
2262                         adapter->nreadblocks[i], adapter->nwriteblocks[i]);
 
2264                 len += sprintf(page+len,
 
2265                         "\tRead errors = %lu, Write errors = %lu\n\n",
 
2266                         adapter->rd_errors[i], adapter->wr_errors[i]);
 
2269         len += sprintf(page+len,
 
2270                         "IO and error counters not compiled in driver.\n");
 
2281  * @page - buffer to write the data in
 
2282  * @start - where the actual data has been written in page
 
2283  * @offset - same meaning as the read system call
 
2284  * @count - same meaning as the read system call
 
2285  * @eof - set if no more data needs to be returned
 
2286  * @data - pointer to our soft state
 
2288  * Display mailbox information for the last command issued. This information
 
2289  * is good for debugging.
 
2292 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
 
2296         adapter_t       *adapter = (adapter_t *)data;
 
2297         volatile mbox_t *mbox = adapter->mbox;
 
2300         len = sprintf(page, "Contents of Mail Box Structure\n");
 
2301         len += sprintf(page+len, "  Fw Command   = 0x%02x\n", 
 
2303         len += sprintf(page+len, "  Cmd Sequence = 0x%02x\n", 
 
2305         len += sprintf(page+len, "  No of Sectors= %04d\n", 
 
2306                         mbox->m_out.numsectors);
 
2307         len += sprintf(page+len, "  LBA          = 0x%02x\n", 
 
2309         len += sprintf(page+len, "  DTA          = 0x%08x\n", 
 
2310                         mbox->m_out.xferaddr);
 
2311         len += sprintf(page+len, "  Logical Drive= 0x%02x\n", 
 
2312                         mbox->m_out.logdrv);
 
2313         len += sprintf(page+len, "  No of SG Elmt= 0x%02x\n",
 
2314                         mbox->m_out.numsgelements);
 
2315         len += sprintf(page+len, "  Busy         = %01x\n", 
 
2317         len += sprintf(page+len, "  Status       = 0x%02x\n", 
 
2327  * proc_rebuild_rate()
 
2328  * @page - buffer to write the data in
 
2329  * @start - where the actual data has been written in page
 
2330  * @offset - same meaning as the read system call
 
2331  * @count - same meaning as the read system call
 
2332  * @eof - set if no more data needs to be returned
 
2333  * @data - pointer to our soft state
 
2335  * Display current rebuild rate
 
2338 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
 
2341         adapter_t       *adapter = (adapter_t *)data;
 
2342         dma_addr_t      dma_handle;
 
2344         struct pci_dev  *pdev;
 
2347         if( make_local_pdev(adapter, &pdev) != 0 ) {
 
2352         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
 
2353                 free_local_pdev(pdev);
 
2358         if( mega_adapinq(adapter, dma_handle) != 0 ) {
 
2360                 len = sprintf(page, "Adapter inquiry failed.\n");
 
2362                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
 
2364                 mega_free_inquiry(inquiry, dma_handle, pdev);
 
2366                 free_local_pdev(pdev);
 
2373         if( adapter->flag & BOARD_40LD ) {
 
2374                 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
 
2375                         ((mega_inquiry3 *)inquiry)->rebuild_rate);
 
2378                 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
 
2379                         ((mraid_ext_inquiry *)
 
2380                         inquiry)->raid_inq.adapter_info.rebuild_rate);
 
2384         mega_free_inquiry(inquiry, dma_handle, pdev);
 
2386         free_local_pdev(pdev);
 
2396  * @page - buffer to write the data in
 
2397  * @start - where the actual data has been written in page
 
2398  * @offset - same meaning as the read system call
 
2399  * @count - same meaning as the read system call
 
2400  * @eof - set if no more data needs to be returned
 
2401  * @data - pointer to our soft state
 
2403  * Display information about the battery module on the controller.
 
2406 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
 
2409         adapter_t       *adapter = (adapter_t *)data;
 
2410         dma_addr_t      dma_handle;
 
2412         struct pci_dev  *pdev;
 
2413         u8      battery_status = 0;
 
2417         if( make_local_pdev(adapter, &pdev) != 0 ) {
 
2422         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
 
2423                 free_local_pdev(pdev);
 
2428         if( mega_adapinq(adapter, dma_handle) != 0 ) {
 
2430                 len = sprintf(page, "Adapter inquiry failed.\n");
 
2432                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
 
2434                 mega_free_inquiry(inquiry, dma_handle, pdev);
 
2436                 free_local_pdev(pdev);
 
2443         if( adapter->flag & BOARD_40LD ) {
 
2444                 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
 
2447                 battery_status = ((mraid_ext_inquiry *)inquiry)->
 
2448                         raid_inq.adapter_info.battery_status;
 
2452          * Decode the battery status
 
2454         sprintf(str, "Battery Status:[%d]", battery_status);
 
2456         if(battery_status == MEGA_BATT_CHARGE_DONE)
 
2457                 strcat(str, " Charge Done");
 
2459         if(battery_status & MEGA_BATT_MODULE_MISSING)
 
2460                 strcat(str, " Module Missing");
 
2462         if(battery_status & MEGA_BATT_LOW_VOLTAGE)
 
2463                 strcat(str, " Low Voltage");
 
2465         if(battery_status & MEGA_BATT_TEMP_HIGH)
 
2466                 strcat(str, " Temperature High");
 
2468         if(battery_status & MEGA_BATT_PACK_MISSING)
 
2469                 strcat(str, " Pack Missing");
 
2471         if(battery_status & MEGA_BATT_CHARGE_INPROG)
 
2472                 strcat(str, " Charge In-progress");
 
2474         if(battery_status & MEGA_BATT_CHARGE_FAIL)
 
2475                 strcat(str, " Charge Fail");
 
2477         if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
 
2478                 strcat(str, " Cycles Exceeded");
 
2480         len = sprintf(page, "%s\n", str);
 
2483         mega_free_inquiry(inquiry, dma_handle, pdev);
 
2485         free_local_pdev(pdev);
 
2495  * @page - buffer to write the data in
 
2496  * @start - where the actual data has been written in page
 
2497  * @offset - same meaning as the read system call
 
2498  * @count - same meaning as the read system call
 
2499  * @eof - set if no more data needs to be returned
 
2500  * @data - pointer to our soft state
 
2502  * Display information about the physical drives on physical channel 0.
 
2505 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
 
2508         adapter_t *adapter = (adapter_t *)data;
 
2512         return (proc_pdrv(adapter, page, 0));
 
2518  * @page - buffer to write the data in
 
2519  * @start - where the actual data has been written in page
 
2520  * @offset - same meaning as the read system call
 
2521  * @count - same meaning as the read system call
 
2522  * @eof - set if no more data needs to be returned
 
2523  * @data - pointer to our soft state
 
2525  * Display information about the physical drives on physical channel 1.
 
2528 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
 
2531         adapter_t *adapter = (adapter_t *)data;
 
2535         return (proc_pdrv(adapter, page, 1));
 
2541  * @page - buffer to write the data in
 
2542  * @start - where the actual data has been written in page
 
2543  * @offset - same meaning as the read system call
 
2544  * @count - same meaning as the read system call
 
2545  * @eof - set if no more data needs to be returned
 
2546  * @data - pointer to our soft state
 
2548  * Display information about the physical drives on physical channel 2.
 
2551 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
 
2554         adapter_t *adapter = (adapter_t *)data;
 
2558         return (proc_pdrv(adapter, page, 2));
 
2564  * @page - buffer to write the data in
 
2565  * @start - where the actual data has been written in page
 
2566  * @offset - same meaning as the read system call
 
2567  * @count - same meaning as the read system call
 
2568  * @eof - set if no more data needs to be returned
 
2569  * @data - pointer to our soft state
 
2571  * Display information about the physical drives on physical channel 3.
 
2574 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
 
2577         adapter_t *adapter = (adapter_t *)data;
 
2581         return (proc_pdrv(adapter, page, 3));
 
2587  * @page - buffer to write the data in
 
2588  * @adapter - pointer to our soft state
 
2590  * Display information about the physical drives.
 
2593 proc_pdrv(adapter_t *adapter, char *page, int channel)
 
2595         dma_addr_t      dma_handle;
 
2597         dma_addr_t      scsi_inq_dma_handle;
 
2599         struct pci_dev  *pdev;
 
2608         if( make_local_pdev(adapter, &pdev) != 0 ) {
 
2612         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
 
2616         if( mega_adapinq(adapter, dma_handle) != 0 ) {
 
2617                 len = sprintf(page, "Adapter inquiry failed.\n");
 
2619                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
 
2625         scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
 
2627         if( scsi_inq == NULL ) {
 
2628                 len = sprintf(page, "memory not available for scsi inq.\n");
 
2633         if( adapter->flag & BOARD_40LD ) {
 
2634                 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
 
2637                 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
 
2638                         raid_inq.pdrv_info.pdrv_state;
 
2641         max_channels = adapter->product_info.nchannels;
 
2643         if( channel >= max_channels ) {
 
2647         for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
 
2649                 i = channel*16 + tgt;
 
2651                 state = *(pdrv_state + i);
 
2653                 switch( state & 0x0F ) {
 
2657                         "Channel:%2d Id:%2d State: Online",
 
2663                         "Channel:%2d Id:%2d State: Failed",
 
2669                         "Channel:%2d Id:%2d State: Rebuild",
 
2675                         "Channel:%2d Id:%2d State: Hot spare",
 
2681                         "Channel:%2d Id:%2d State: Un-configured",
 
2688                  * This interface displays inquiries for disk drives
 
2689                  * only. Inquries for logical drives and non-disk
 
2690                  * devices are available through /proc/scsi/scsi
 
2692                 memset(scsi_inq, 0, 256);
 
2693                 if( mega_internal_dev_inquiry(adapter, channel, tgt,
 
2694                                 scsi_inq_dma_handle) ||
 
2695                                 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
 
2700                  * Check for overflow. We print less than 240
 
2701                  * characters for inquiry
 
2703                 if( (len + 240) >= PAGE_SIZE ) break;
 
2705                 len += sprintf(page+len, "%s.\n", str);
 
2707                 len += mega_print_inquiry(page+len, scsi_inq);
 
2711         pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
 
2713         mega_free_inquiry(inquiry, dma_handle, pdev);
 
2715         free_local_pdev(pdev);
 
2722  * Display scsi inquiry
 
2725 mega_print_inquiry(char *page, char *scsi_inq)
 
2730         len = sprintf(page, "  Vendor: ");
 
2731         for( i = 8; i < 16; i++ ) {
 
2732                 len += sprintf(page+len, "%c", scsi_inq[i]);
 
2735         len += sprintf(page+len, "  Model: ");
 
2737         for( i = 16; i < 32; i++ ) {
 
2738                 len += sprintf(page+len, "%c", scsi_inq[i]);
 
2741         len += sprintf(page+len, "  Rev: ");
 
2743         for( i = 32; i < 36; i++ ) {
 
2744                 len += sprintf(page+len, "%c", scsi_inq[i]);
 
2747         len += sprintf(page+len, "\n");
 
2749         i = scsi_inq[0] & 0x1f;
 
2751         len += sprintf(page+len, "  Type:   %s ", scsi_device_type(i));
 
2753         len += sprintf(page+len,
 
2754         "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
 
2756         if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
 
2757                 len += sprintf(page+len, " CCS\n");
 
2759                 len += sprintf(page+len, "\n");
 
2767  * @page - buffer to write the data in
 
2768  * @start - where the actual data has been written in page
 
2769  * @offset - same meaning as the read system call
 
2770  * @count - same meaning as the read system call
 
2771  * @eof - set if no more data needs to be returned
 
2772  * @data - pointer to our soft state
 
2774  * Display real time information about the logical drives 0 through 9.
 
2777 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
 
2780         adapter_t *adapter = (adapter_t *)data;
 
2784         return (proc_rdrv(adapter, page, 0, 9));
 
2790  * @page - buffer to write the data in
 
2791  * @start - where the actual data has been written in page
 
2792  * @offset - same meaning as the read system call
 
2793  * @count - same meaning as the read system call
 
2794  * @eof - set if no more data needs to be returned
 
2795  * @data - pointer to our soft state
 
2797  * Display real time information about the logical drives 0 through 9.
 
2800 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
 
2803         adapter_t *adapter = (adapter_t *)data;
 
2807         return (proc_rdrv(adapter, page, 10, 19));
 
2813  * @page - buffer to write the data in
 
2814  * @start - where the actual data has been written in page
 
2815  * @offset - same meaning as the read system call
 
2816  * @count - same meaning as the read system call
 
2817  * @eof - set if no more data needs to be returned
 
2818  * @data - pointer to our soft state
 
2820  * Display real time information about the logical drives 0 through 9.
 
2823 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
 
2826         adapter_t *adapter = (adapter_t *)data;
 
2830         return (proc_rdrv(adapter, page, 20, 29));
 
2836  * @page - buffer to write the data in
 
2837  * @start - where the actual data has been written in page
 
2838  * @offset - same meaning as the read system call
 
2839  * @count - same meaning as the read system call
 
2840  * @eof - set if no more data needs to be returned
 
2841  * @data - pointer to our soft state
 
2843  * Display real time information about the logical drives 0 through 9.
 
2846 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
 
2849         adapter_t *adapter = (adapter_t *)data;
 
2853         return (proc_rdrv(adapter, page, 30, 39));
 
2859  * @page - buffer to write the data in
 
2860  * @adapter - pointer to our soft state
 
2861  * @start - starting logical drive to display
 
2862  * @end - ending logical drive to display
 
2864  * We do not print the inquiry information since its already available through
 
2865  * /proc/scsi/scsi interface
 
2868 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
 
2870         dma_addr_t      dma_handle;
 
2871         logdrv_param    *lparam;
 
2874         dma_addr_t      disk_array_dma_handle;
 
2876         struct pci_dev  *pdev;
 
2883         if( make_local_pdev(adapter, &pdev) != 0 ) {
 
2887         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
 
2888                 free_local_pdev(pdev);
 
2892         if( mega_adapinq(adapter, dma_handle) != 0 ) {
 
2894                 len = sprintf(page, "Adapter inquiry failed.\n");
 
2896                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
 
2898                 mega_free_inquiry(inquiry, dma_handle, pdev);
 
2900                 free_local_pdev(pdev);
 
2905         memset(&mc, 0, sizeof(megacmd_t));
 
2907         if( adapter->flag & BOARD_40LD ) {
 
2908                 array_sz = sizeof(disk_array_40ld);
 
2910                 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
 
2912                 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
 
2915                 array_sz = sizeof(disk_array_8ld);
 
2917                 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
 
2918                         raid_inq.logdrv_info.ldrv_state;
 
2920                 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
 
2921                         raid_inq.logdrv_info.num_ldrv;
 
2924         disk_array = pci_alloc_consistent(pdev, array_sz,
 
2925                         &disk_array_dma_handle);
 
2927         if( disk_array == NULL ) {
 
2928                 len = sprintf(page, "memory not available.\n");
 
2930                 mega_free_inquiry(inquiry, dma_handle, pdev);
 
2932                 free_local_pdev(pdev);
 
2937         mc.xferaddr = (u32)disk_array_dma_handle;
 
2939         if( adapter->flag & BOARD_40LD ) {
 
2940                 mc.cmd = FC_NEW_CONFIG;
 
2941                 mc.opcode = OP_DCMD_READ_CONFIG;
 
2943                 if( mega_internal_command(adapter, &mc, NULL) ) {
 
2945                         len = sprintf(page, "40LD read config failed.\n");
 
2947                         mega_free_inquiry(inquiry, dma_handle, pdev);
 
2949                         pci_free_consistent(pdev, array_sz, disk_array,
 
2950                                         disk_array_dma_handle);
 
2952                         free_local_pdev(pdev);
 
2959                 mc.cmd = NEW_READ_CONFIG_8LD;
 
2961                 if( mega_internal_command(adapter, &mc, NULL) ) {
 
2963                         mc.cmd = READ_CONFIG_8LD;
 
2965                         if( mega_internal_command(adapter, &mc,
 
2969                                         "8LD read config failed.\n");
 
2971                                 mega_free_inquiry(inquiry, dma_handle, pdev);
 
2973                                 pci_free_consistent(pdev, array_sz,
 
2975                                                 disk_array_dma_handle);
 
2977                                 free_local_pdev(pdev);
 
2984         for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
 
2986                 if( adapter->flag & BOARD_40LD ) {
 
2988                         &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
 
2992                         &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
 
2996                  * Check for overflow. We print less than 240 characters for
 
2997                  * information about each logical drive.
 
2999                 if( (len + 240) >= PAGE_SIZE ) break;
 
3001                 len += sprintf(page+len, "Logical drive:%2d:, ", i);
 
3003                 switch( rdrv_state[i] & 0x0F ) {
 
3005                         len += sprintf(page+len, "state: offline");
 
3009                         len += sprintf(page+len, "state: degraded");
 
3013                         len += sprintf(page+len, "state: optimal");
 
3017                         len += sprintf(page+len, "state: deleted");
 
3021                         len += sprintf(page+len, "state: unknown");
 
3026                  * Check if check consistency or initialization is going on
 
3027                  * for this logical drive.
 
3029                 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
 
3030                         len += sprintf(page+len,
 
3031                                         ", check-consistency in progress");
 
3033                 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
 
3034                         len += sprintf(page+len,
 
3035                                         ", initialization in progress");
 
3038                 len += sprintf(page+len, "\n");
 
3040                 len += sprintf(page+len, "Span depth:%3d, ",
 
3041                                 lparam->span_depth);
 
3043                 len += sprintf(page+len, "RAID level:%3d, ",
 
3046                 len += sprintf(page+len, "Stripe size:%3d, ",
 
3047                                 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
 
3049                 len += sprintf(page+len, "Row size:%3d\n",
 
3053                 len += sprintf(page+len, "Read Policy: ");
 
3055                 switch(lparam->read_ahead) {
 
3058                         len += sprintf(page+len, "No read ahead, ");
 
3062                         len += sprintf(page+len, "Read ahead, ");
 
3065                 case ADAP_READ_AHEAD:
 
3066                         len += sprintf(page+len, "Adaptive, ");
 
3071                 len += sprintf(page+len, "Write Policy: ");
 
3073                 switch(lparam->write_mode) {
 
3075                 case WRMODE_WRITE_THRU:
 
3076                         len += sprintf(page+len, "Write thru, ");
 
3079                 case WRMODE_WRITE_BACK:
 
3080                         len += sprintf(page+len, "Write back, ");
 
3084                 len += sprintf(page+len, "Cache Policy: ");
 
3086                 switch(lparam->direct_io) {
 
3089                         len += sprintf(page+len, "Cached IO\n\n");
 
3093                         len += sprintf(page+len, "Direct IO\n\n");
 
3098         mega_free_inquiry(inquiry, dma_handle, pdev);
 
3100         pci_free_consistent(pdev, array_sz, disk_array,
 
3101                         disk_array_dma_handle);
 
3103         free_local_pdev(pdev);
 
3108 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
 
3115  * megaraid_biosparam()
 
3117  * Return the disk geometry for a particular disk
 
3120 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
 
3121                     sector_t capacity, int geom[])
 
3130         /* Get pointer to host config structure */
 
3131         adapter = (adapter_t *)sdev->host->hostdata;
 
3133         if (IS_RAID_CH(adapter, sdev->channel)) {
 
3134                         /* Default heads (64) & sectors (32) */
 
3137                         cylinders = (ulong)capacity / (heads * sectors);
 
3140                          * Handle extended translation size for logical drives
 
3143                         if ((ulong)capacity >= 0x200000) {
 
3146                                 cylinders = (ulong)capacity / (heads * sectors);
 
3152                         geom[2] = cylinders;
 
3155                 bh = scsi_bios_ptable(bdev);
 
3158                         rval = scsi_partsize(bh, capacity,
 
3159                                             &geom[2], &geom[0], &geom[1]);
 
3166                 "megaraid: invalid partition on this disk on channel %d\n",
 
3169                 /* Default heads (64) & sectors (32) */
 
3172                 cylinders = (ulong)capacity / (heads * sectors);
 
3174                 /* Handle extended translation size for logical drives > 1Gb */
 
3175                 if ((ulong)capacity >= 0x200000) {
 
3178                         cylinders = (ulong)capacity / (heads * sectors);
 
3184                 geom[2] = cylinders;
 
3192  * @adapter - pointer to our soft state
 
3194  * Allocate memory for the various pointers in the scb structures:
 
3195  * scatter-gather list pointer, passthru and extended passthru structure
 
3199 mega_init_scb(adapter_t *adapter)
 
3204         for( i = 0; i < adapter->max_cmds; i++ ) {
 
3206                 scb = &adapter->scb_list[i];
 
3214         for( i = 0; i < adapter->max_cmds; i++ ) {
 
3216                 scb = &adapter->scb_list[i];
 
3220                 scb->sgl64 = pci_alloc_consistent(adapter->dev,
 
3221                                 sizeof(mega_sgl64) * adapter->sglen,
 
3222                                 &scb->sgl_dma_addr);
 
3224                 scb->sgl = (mega_sglist *)scb->sgl64;
 
3227                         printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
 
3228                         mega_free_sgl(adapter);
 
3232                 scb->pthru = pci_alloc_consistent(adapter->dev,
 
3233                                 sizeof(mega_passthru),
 
3234                                 &scb->pthru_dma_addr);
 
3237                         printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
 
3238                         mega_free_sgl(adapter);
 
3242                 scb->epthru = pci_alloc_consistent(adapter->dev,
 
3243                                 sizeof(mega_ext_passthru),
 
3244                                 &scb->epthru_dma_addr);
 
3246                 if( !scb->epthru ) {
 
3248                                 "Can't allocate extended passthru.\n");
 
3249                         mega_free_sgl(adapter);
 
3254                 scb->dma_type = MEGA_DMA_TYPE_NONE;
 
3258                  * lock not required since we are loading the driver, so no
 
3259                  * commands possible right now.
 
3261                 scb->state = SCB_FREE;
 
3263                 list_add(&scb->list, &adapter->free_list);
 
3275  * Routines for the character/ioctl interface to the driver. Find out if this
 
3279 megadev_open (struct inode *inode, struct file *filep)
 
3281         cycle_kernel_lock();
 
3283          * Only allow superuser to access private ioctl interface
 
3285         if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
 
3293  * @inode - Our device inode
 
3295  * @cmd - ioctl command
 
3296  * @arg - user buffer
 
3298  * ioctl entry point for our private ioctl interface. We move the data in from
 
3299  * the user space, prepare the command (if necessary, convert the old MIMD
 
3300  * ioctl to new ioctl command), and issue a synchronous command to the
 
3304 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
 
3311         mega_passthru   __user *upthru; /* user address for passthru */
 
3312         mega_passthru   *pthru;         /* copy user passthru here */
 
3313         dma_addr_t      pthru_dma_hndl;
 
3314         void            *data = NULL;   /* data to be transferred */
 
3315         dma_addr_t      data_dma_hndl;  /* dma handle for data xfer area */
 
3317         megastat_t      __user *ustats;
 
3320         struct pci_dev  *pdev;
 
3322         ustats = NULL; /* avoid compilation warnings */
 
3326          * Make sure only USCSICMD are issued through this interface.
 
3327          * MIMD application would still fire different command.
 
3329         if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
 
3334          * Check and convert a possible MIMD command to NIT command.
 
3335          * mega_m_to_n() copies the data from the user space, so we do not
 
3336          * have to do it here.
 
3337          * NOTE: We will need some user address to copyout the data, therefore
 
3338          * the inteface layer will also provide us with the required user
 
3341         memset(&uioc, 0, sizeof(nitioctl_t));
 
3342         if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
 
3346         switch( uioc.opcode ) {
 
3348         case GET_DRIVER_VER:
 
3349                 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
 
3355                 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
 
3359                  * Shucks. MIMD interface returns a positive value for number
 
3360                  * of adapters. TODO: Change it to return 0 when there is no
 
3361                  * applicatio using mimd interface.
 
3370                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
 
3373                 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
 
3374                                 sizeof(struct mcontroller)) )
 
3384                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
 
3387                 adapter = hba_soft_state[adapno];
 
3389                 ustats = uioc.uioc_uaddr;
 
3391                 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
 
3395                  * Check for the validity of the logical drive number
 
3397                 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
 
3399                 if( copy_to_user(ustats->nreads, adapter->nreads,
 
3400                                         num_ldrv*sizeof(u32)) )
 
3403                 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
 
3404                                         num_ldrv*sizeof(u32)) )
 
3407                 if( copy_to_user(ustats->nwrites, adapter->nwrites,
 
3408                                         num_ldrv*sizeof(u32)) )
 
3411                 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
 
3412                                         num_ldrv*sizeof(u32)) )
 
3415                 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
 
3416                                         num_ldrv*sizeof(u32)) )
 
3419                 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
 
3420                                         num_ldrv*sizeof(u32)) )
 
3431                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
 
3434                 adapter = hba_soft_state[adapno];
 
3437                  * Deletion of logical drive is a special case. The adapter
 
3438                  * should be quiescent before this command is issued.
 
3440                 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
 
3441                                 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
 
3444                          * Do we support this feature
 
3446                         if( !adapter->support_random_del ) {
 
3447                                 printk(KERN_WARNING "megaraid: logdrv ");
 
3448                                 printk("delete on non-supporting F/W.\n");
 
3453                         rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
 
3456                                 memset(&mc, 0, sizeof(megacmd_t));
 
3460                                 rval = mega_n_to_m((void __user *)arg, &mc);
 
3466                  * This interface only support the regular passthru commands.
 
3467                  * Reject extended passthru and 64-bit passthru
 
3469                 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
 
3470                         uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
 
3472                         printk(KERN_WARNING "megaraid: rejected passthru.\n");
 
3478                  * For all internal commands, the buffer must be allocated in
 
3479                  * <4GB address range
 
3481                 if( make_local_pdev(adapter, &pdev) != 0 )
 
3484                 /* Is it a passthru command or a DCMD */
 
3485                 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
 
3486                         /* Passthru commands */
 
3488                         pthru = pci_alloc_consistent(pdev,
 
3489                                         sizeof(mega_passthru),
 
3492                         if( pthru == NULL ) {
 
3493                                 free_local_pdev(pdev);
 
3498                          * The user passthru structure
 
3500                         upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
 
3503                          * Copy in the user passthru here.
 
3505                         if( copy_from_user(pthru, upthru,
 
3506                                                 sizeof(mega_passthru)) ) {
 
3508                                 pci_free_consistent(pdev,
 
3509                                                 sizeof(mega_passthru), pthru,
 
3512                                 free_local_pdev(pdev);
 
3518                          * Is there a data transfer
 
3520                         if( pthru->dataxferlen ) {
 
3521                                 data = pci_alloc_consistent(pdev,
 
3525                                 if( data == NULL ) {
 
3526                                         pci_free_consistent(pdev,
 
3527                                                         sizeof(mega_passthru),
 
3531                                         free_local_pdev(pdev);
 
3537                                  * Save the user address and point the kernel
 
3538                                  * address at just allocated memory
 
3540                                 uxferaddr = pthru->dataxferaddr;
 
3541                                 pthru->dataxferaddr = data_dma_hndl;
 
3546                          * Is data coming down-stream
 
3548                         if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
 
3552                                 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
 
3553                                                         pthru->dataxferlen) ) {
 
3555                                         goto freemem_and_return;
 
3559                         memset(&mc, 0, sizeof(megacmd_t));
 
3561                         mc.cmd = MEGA_MBOXCMD_PASSTHRU;
 
3562                         mc.xferaddr = (u32)pthru_dma_hndl;
 
3567                         mega_internal_command(adapter, &mc, pthru);
 
3569                         rval = mega_n_to_m((void __user *)arg, &mc);
 
3571                         if( rval ) goto freemem_and_return;
 
3575                          * Is data going up-stream
 
3577                         if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
 
3578                                 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
 
3579                                                         pthru->dataxferlen) ) {
 
3585                          * Send the request sense data also, irrespective of
 
3586                          * whether the user has asked for it or not.
 
3588                         if (copy_to_user(upthru->reqsensearea,
 
3589                                         pthru->reqsensearea, 14))
 
3593                         if( pthru->dataxferlen ) {
 
3594                                 pci_free_consistent(pdev,
 
3595                                                 pthru->dataxferlen, data,
 
3599                         pci_free_consistent(pdev, sizeof(mega_passthru),
 
3600                                         pthru, pthru_dma_hndl);
 
3602                         free_local_pdev(pdev);
 
3610                          * Is there a data transfer
 
3612                         if( uioc.xferlen ) {
 
3613                                 data = pci_alloc_consistent(pdev,
 
3614                                                 uioc.xferlen, &data_dma_hndl);
 
3616                                 if( data == NULL ) {
 
3617                                         free_local_pdev(pdev);
 
3621                                 uxferaddr = MBOX(uioc)->xferaddr;
 
3625                          * Is data coming down-stream
 
3627                         if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
 
3631                                 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
 
3634                                         pci_free_consistent(pdev,
 
3636                                                         data, data_dma_hndl);
 
3638                                         free_local_pdev(pdev);
 
3644                         memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
 
3646                         mc.xferaddr = (u32)data_dma_hndl;
 
3651                         mega_internal_command(adapter, &mc, NULL);
 
3653                         rval = mega_n_to_m((void __user *)arg, &mc);
 
3656                                 if( uioc.xferlen ) {
 
3657                                         pci_free_consistent(pdev,
 
3662                                 free_local_pdev(pdev);
 
3668                          * Is data going up-stream
 
3670                         if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
 
3671                                 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
 
3678                         if( uioc.xferlen ) {
 
3679                                 pci_free_consistent(pdev,
 
3684                         free_local_pdev(pdev);
 
3698  * @arg - user address
 
3699  * @uioc - new ioctl structure
 
3701  * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
 
3704  * Converts the older mimd ioctl structure to newer NIT structure
 
3707 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
 
3709         struct uioctl_t uioc_mimd;
 
3710         char    signature[8] = {0};
 
3716          * check is the application conforms to NIT. We do not have to do much
 
3718          * We exploit the fact that the signature is stored in the very
 
3719          * begining of the structure.
 
3722         if( copy_from_user(signature, arg, 7) )
 
3725         if( memcmp(signature, "MEGANIT", 7) == 0 ) {
 
3728                  * NOTE NOTE: The nit ioctl is still under flux because of
 
3729                  * change of mailbox definition, in HPE. No applications yet
 
3730                  * use this interface and let's not have applications use this
 
3731                  * interface till the new specifitions are in place.
 
3735                 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
 
3742          * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
 
3744          * Get the user ioctl structure
 
3746         if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
 
3751          * Get the opcode and subopcode for the commands
 
3753         opcode = uioc_mimd.ui.fcs.opcode;
 
3754         subopcode = uioc_mimd.ui.fcs.subopcode;
 
3759                 switch (subopcode) {
 
3761                 case MEGAIOC_QDRVRVER:  /* Query driver version */
 
3762                         uioc->opcode = GET_DRIVER_VER;
 
3763                         uioc->uioc_uaddr = uioc_mimd.data;
 
3766                 case MEGAIOC_QNADAP:    /* Get # of adapters */
 
3767                         uioc->opcode = GET_N_ADAP;
 
3768                         uioc->uioc_uaddr = uioc_mimd.data;
 
3771                 case MEGAIOC_QADAPINFO: /* Get adapter information */
 
3772                         uioc->opcode = GET_ADAP_INFO;
 
3773                         uioc->adapno = uioc_mimd.ui.fcs.adapno;
 
3774                         uioc->uioc_uaddr = uioc_mimd.data;
 
3786                 uioc->opcode = MBOX_CMD;
 
3787                 uioc->adapno = uioc_mimd.ui.fcs.adapno;
 
3789                 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
 
3791                 uioc->xferlen = uioc_mimd.ui.fcs.length;
 
3793                 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
 
3794                 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
 
3800                 uioc->opcode = MBOX_CMD;
 
3801                 uioc->adapno = uioc_mimd.ui.fcs.adapno;
 
3803                 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
 
3806                  * Choose the xferlen bigger of input and output data
 
3808                 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
 
3809                         uioc_mimd.outlen : uioc_mimd.inlen;
 
3811                 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
 
3812                 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
 
3826  * @arg - user address
 
3827  * @mc - mailbox command
 
3829  * Updates the status information to the application, depending on application
 
3830  * conforms to older mimd ioctl interface or newer NIT ioctl interface
 
3833 mega_n_to_m(void __user *arg, megacmd_t *mc)
 
3835         nitioctl_t      __user *uiocp;
 
3836         megacmd_t       __user *umc;
 
3837         mega_passthru   __user *upthru;
 
3838         struct uioctl_t __user *uioc_mimd;
 
3839         char    signature[8] = {0};
 
3842          * check is the application conforms to NIT.
 
3844         if( copy_from_user(signature, arg, 7) )
 
3847         if( memcmp(signature, "MEGANIT", 7) == 0 ) {
 
3851                 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
 
3854                 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
 
3856                         umc = MBOX_P(uiocp);
 
3858                         if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
 
3861                         if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
 
3868                 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
 
3871                 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
 
3873                         umc = (megacmd_t __user *)uioc_mimd->mbox;
 
3875                         if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
 
3878                         if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
 
3888  * MEGARAID 'FW' commands.
 
3892  * mega_is_bios_enabled()
 
3893  * @adapter - pointer to our soft state
 
3895  * issue command to find out if the BIOS is enabled for this controller
 
3898 mega_is_bios_enabled(adapter_t *adapter)
 
3900         unsigned char   raw_mbox[sizeof(struct mbox_out)];
 
3904         mbox = (mbox_t *)raw_mbox;
 
3906         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
3908         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
3910         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
3912         raw_mbox[0] = IS_BIOS_ENABLED;
 
3913         raw_mbox[2] = GET_BIOS;
 
3916         ret = issue_scb_block(adapter, raw_mbox);
 
3918         return *(char *)adapter->mega_buffer;
 
3923  * mega_enum_raid_scsi()
 
3924  * @adapter - pointer to our soft state
 
3926  * Find out what channels are RAID/SCSI. This information is used to
 
3927  * differentiate the virtual channels and physical channels and to support
 
3928  * ROMB feature and non-disk devices.
 
3931 mega_enum_raid_scsi(adapter_t *adapter)
 
3933         unsigned char raw_mbox[sizeof(struct mbox_out)];
 
3937         mbox = (mbox_t *)raw_mbox;
 
3939         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
3942          * issue command to find out what channels are raid/scsi
 
3944         raw_mbox[0] = CHNL_CLASS;
 
3945         raw_mbox[2] = GET_CHNL_CLASS;
 
3947         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
3949         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
3952          * Non-ROMB firmware fail this command, so all channels
 
3953          * must be shown RAID
 
3955         adapter->mega_ch_class = 0xFF;
 
3957         if(!issue_scb_block(adapter, raw_mbox)) {
 
3958                 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
 
3962         for( i = 0; i < adapter->product_info.nchannels; i++ ) { 
 
3963                 if( (adapter->mega_ch_class >> i) & 0x01 ) {
 
3964                         printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
 
3968                         printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
 
3978  * mega_get_boot_drv()
 
3979  * @adapter - pointer to our soft state
 
3981  * Find out which device is the boot device. Note, any logical drive or any
 
3982  * phyical device (e.g., a CDROM) can be designated as a boot device.
 
3985 mega_get_boot_drv(adapter_t *adapter)
 
3987         struct private_bios_data        *prv_bios_data;
 
3988         unsigned char   raw_mbox[sizeof(struct mbox_out)];
 
3995         mbox = (mbox_t *)raw_mbox;
 
3997         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
3999         raw_mbox[0] = BIOS_PVT_DATA;
 
4000         raw_mbox[2] = GET_BIOS_PVT_DATA;
 
4002         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
4004         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
4006         adapter->boot_ldrv_enabled = 0;
 
4007         adapter->boot_ldrv = 0;
 
4009         adapter->boot_pdrv_enabled = 0;
 
4010         adapter->boot_pdrv_ch = 0;
 
4011         adapter->boot_pdrv_tgt = 0;
 
4013         if(issue_scb_block(adapter, raw_mbox) == 0) {
 
4015                         (struct private_bios_data *)adapter->mega_buffer;
 
4018                 cksum_p = (char *)prv_bios_data;
 
4019                 for (i = 0; i < 14; i++ ) {
 
4020                         cksum += (u16)(*cksum_p++);
 
4023                 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
 
4026                          * If MSB is set, a physical drive is set as boot
 
4029                         if( prv_bios_data->boot_drv & 0x80 ) {
 
4030                                 adapter->boot_pdrv_enabled = 1;
 
4031                                 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
 
4032                                 adapter->boot_pdrv_ch = boot_pdrv / 16;
 
4033                                 adapter->boot_pdrv_tgt = boot_pdrv % 16;
 
4036                                 adapter->boot_ldrv_enabled = 1;
 
4037                                 adapter->boot_ldrv = prv_bios_data->boot_drv;
 
4045  * mega_support_random_del()
 
4046  * @adapter - pointer to our soft state
 
4048  * Find out if this controller supports random deletion and addition of
 
4052 mega_support_random_del(adapter_t *adapter)
 
4054         unsigned char raw_mbox[sizeof(struct mbox_out)];
 
4058         mbox = (mbox_t *)raw_mbox;
 
4060         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
4065         raw_mbox[0] = FC_DEL_LOGDRV;
 
4066         raw_mbox[2] = OP_SUP_DEL_LOGDRV;
 
4068         rval = issue_scb_block(adapter, raw_mbox);
 
4075  * mega_support_ext_cdb()
 
4076  * @adapter - pointer to our soft state
 
4078  * Find out if this firmware support cdblen > 10
 
4081 mega_support_ext_cdb(adapter_t *adapter)
 
4083         unsigned char raw_mbox[sizeof(struct mbox_out)];
 
4087         mbox = (mbox_t *)raw_mbox;
 
4089         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
4091          * issue command to find out if controller supports extended CDBs.
 
4096         rval = issue_scb_block(adapter, raw_mbox);
 
4104  * @adapter - pointer to our soft state
 
4105  * @logdrv - logical drive to be deleted
 
4107  * Delete the specified logical drive. It is the responsibility of the user
 
4108  * app to let the OS know about this operation.
 
4111 mega_del_logdrv(adapter_t *adapter, int logdrv)
 
4113         unsigned long flags;
 
4118          * Stop sending commands to the controller, queue them internally.
 
4119          * When deletion is complete, ISR will flush the queue.
 
4121         atomic_set(&adapter->quiescent, 1);
 
4124          * Wait till all the issued commands are complete and there are no
 
4125          * commands in the pending queue
 
4127         while (atomic_read(&adapter->pend_cmds) > 0 ||
 
4128                !list_empty(&adapter->pending_list))
 
4129                 msleep(1000);   /* sleep for 1s */
 
4131         rval = mega_do_del_logdrv(adapter, logdrv);
 
4133         spin_lock_irqsave(&adapter->lock, flags);
 
4136          * If delete operation was successful, add 0x80 to the logical drive
 
4137          * ids for commands in the pending queue.
 
4139         if (adapter->read_ldidmap) {
 
4140                 struct list_head *pos;
 
4141                 list_for_each(pos, &adapter->pending_list) {
 
4142                         scb = list_entry(pos, scb_t, list);
 
4143                         if (scb->pthru->logdrv < 0x80 )
 
4144                                 scb->pthru->logdrv += 0x80;
 
4148         atomic_set(&adapter->quiescent, 0);
 
4150         mega_runpendq(adapter);
 
4152         spin_unlock_irqrestore(&adapter->lock, flags);
 
4159 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
 
4164         memset( &mc, 0, sizeof(megacmd_t));
 
4166         mc.cmd = FC_DEL_LOGDRV;
 
4167         mc.opcode = OP_DEL_LOGDRV;
 
4168         mc.subopcode = logdrv;
 
4170         rval = mega_internal_command(adapter, &mc, NULL);
 
4172         /* log this event */
 
4174                 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
 
4179          * After deleting first logical drive, the logical drives must be
 
4180          * addressed by adding 0x80 to the logical drive id.
 
4182         adapter->read_ldidmap = 1;
 
4189  * mega_get_max_sgl()
 
4190  * @adapter - pointer to our soft state
 
4192  * Find out the maximum number of scatter-gather elements supported by this
 
4193  * version of the firmware
 
4196 mega_get_max_sgl(adapter_t *adapter)
 
4198         unsigned char   raw_mbox[sizeof(struct mbox_out)];
 
4201         mbox = (mbox_t *)raw_mbox;
 
4203         memset(mbox, 0, sizeof(raw_mbox));
 
4205         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
4207         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
4209         raw_mbox[0] = MAIN_MISC_OPCODE;
 
4210         raw_mbox[2] = GET_MAX_SG_SUPPORT;
 
4213         if( issue_scb_block(adapter, raw_mbox) ) {
 
4215                  * f/w does not support this command. Choose the default value
 
4217                 adapter->sglen = MIN_SGLIST;
 
4220                 adapter->sglen = *((char *)adapter->mega_buffer);
 
4223                  * Make sure this is not more than the resources we are
 
4224                  * planning to allocate
 
4226                 if ( adapter->sglen > MAX_SGLIST )
 
4227                         adapter->sglen = MAX_SGLIST;
 
4235  * mega_support_cluster()
 
4236  * @adapter - pointer to our soft state
 
4238  * Find out if this firmware support cluster calls.
 
4241 mega_support_cluster(adapter_t *adapter)
 
4243         unsigned char   raw_mbox[sizeof(struct mbox_out)];
 
4246         mbox = (mbox_t *)raw_mbox;
 
4248         memset(mbox, 0, sizeof(raw_mbox));
 
4250         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
 
4252         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
 
4255          * Try to get the initiator id. This command will succeed iff the
 
4256          * clustering is available on this HBA.
 
4258         raw_mbox[0] = MEGA_GET_TARGET_ID;
 
4260         if( issue_scb_block(adapter, raw_mbox) == 0 ) {
 
4263                  * Cluster support available. Get the initiator target id.
 
4264                  * Tell our id to mid-layer too.
 
4266                 adapter->this_id = *(u32 *)adapter->mega_buffer;
 
4267                 adapter->host->this_id = adapter->this_id;
 
4275 #ifdef CONFIG_PROC_FS
 
4278  * @adapter - pointer to our soft state
 
4279  * @dma_handle - DMA address of the buffer
 
4281  * Issue internal comamnds while interrupts are available.
 
4282  * We only issue direct mailbox commands from within the driver. ioctl()
 
4283  * interface using these routines can issue passthru commands.
 
4286 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
 
4290         memset(&mc, 0, sizeof(megacmd_t));
 
4292         if( adapter->flag & BOARD_40LD ) {
 
4293                 mc.cmd = FC_NEW_CONFIG;
 
4294                 mc.opcode = NC_SUBOP_ENQUIRY3;
 
4295                 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
 
4298                 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
 
4301         mc.xferaddr = (u32)dma_handle;
 
4303         if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
 
4311 /** mega_internal_dev_inquiry()
 
4312  * @adapter - pointer to our soft state
 
4313  * @ch - channel for this device
 
4314  * @tgt - ID of this device
 
4315  * @buf_dma_handle - DMA address of the buffer
 
4317  * Issue the scsi inquiry for the specified device.
 
4320 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
 
4321                 dma_addr_t buf_dma_handle)
 
4323         mega_passthru   *pthru;
 
4324         dma_addr_t      pthru_dma_handle;
 
4327         struct pci_dev  *pdev;
 
4331          * For all internal commands, the buffer must be allocated in <4GB
 
4334         if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
 
4336         pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
 
4339         if( pthru == NULL ) {
 
4340                 free_local_pdev(pdev);
 
4346         pthru->reqsenselen = 14;
 
4347         pthru->islogical = 0;
 
4349         pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
 
4351         pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
 
4355         pthru->cdb[0] = INQUIRY;
 
4359         pthru->cdb[4] = 255;
 
4363         pthru->dataxferaddr = (u32)buf_dma_handle;
 
4364         pthru->dataxferlen = 256;
 
4366         memset(&mc, 0, sizeof(megacmd_t));
 
4368         mc.cmd = MEGA_MBOXCMD_PASSTHRU;
 
4369         mc.xferaddr = (u32)pthru_dma_handle;
 
4371         rval = mega_internal_command(adapter, &mc, pthru);
 
4373         pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
 
4376         free_local_pdev(pdev);
 
4383  * mega_internal_command()
 
4384  * @adapter - pointer to our soft state
 
4385  * @mc - the mailbox command
 
4386  * @pthru - Passthru structure for DCDB commands
 
4388  * Issue the internal commands in interrupt mode.
 
4389  * The last argument is the address of the passthru structure if the command
 
4390  * to be fired is a passthru command
 
4392  * lockscope specifies whether the caller has already acquired the lock. Of
 
4393  * course, the caller must know which lock we are talking about.
 
4395  * Note: parameter 'pthru' is null for non-passthru commands.
 
4398 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
 
4401         struct  scsi_device *sdev;
 
4405         scmd = scsi_allocate_command(GFP_KERNEL);
 
4410          * The internal commands share one command id and hence are
 
4411          * serialized. This is so because we want to reserve maximum number of
 
4412          * available command ids for the I/O commands.
 
4414         mutex_lock(&adapter->int_mtx);
 
4416         scb = &adapter->int_scb;
 
4417         memset(scb, 0, sizeof(scb_t));
 
4419         sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
 
4420         scmd->device = sdev;
 
4422         memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
 
4423         scmd->cmnd = adapter->int_cdb;
 
4424         scmd->device->host = adapter->host;
 
4425         scmd->host_scribble = (void *)scb;
 
4426         scmd->cmnd[0] = MEGA_INTERNAL_CMD;
 
4428         scb->state |= SCB_ACTIVE;
 
4431         memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
 
4434          * Is it a passthru command
 
4436         if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
 
4441         scb->idx = CMDID_INT_CMDS;
 
4443         megaraid_queue(scmd, mega_internal_done);
 
4445         wait_for_completion(&adapter->int_waitq);
 
4447         rval = scmd->result;
 
4448         mc->status = scmd->result;
 
4452          * Print a debug message for all failed commands. Applications can use
 
4455         if( scmd->result && trace_level ) {
 
4456                 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
 
4457                         mc->cmd, mc->opcode, mc->subopcode, scmd->result);
 
4460         mutex_unlock(&adapter->int_mtx);
 
4462         scsi_free_command(GFP_KERNEL, scmd);
 
4469  * mega_internal_done()
 
4470  * @scmd - internal scsi command
 
4472  * Callback routine for internal commands.
 
4475 mega_internal_done(Scsi_Cmnd *scmd)
 
4479         adapter = (adapter_t *)scmd->device->host->hostdata;
 
4481         complete(&adapter->int_waitq);
 
4486 static struct scsi_host_template megaraid_template = {
 
4487         .module                         = THIS_MODULE,
 
4489         .proc_name                      = "megaraid_legacy",
 
4490         .info                           = megaraid_info,
 
4491         .queuecommand                   = megaraid_queue,       
 
4492         .bios_param                     = megaraid_biosparam,
 
4493         .max_sectors                    = MAX_SECTORS_PER_IO,
 
4494         .can_queue                      = MAX_COMMANDS,
 
4495         .this_id                        = DEFAULT_INITIATOR_ID,
 
4496         .sg_tablesize                   = MAX_SGLIST,
 
4497         .cmd_per_lun                    = DEF_CMD_PER_LUN,
 
4498         .use_clustering                 = ENABLE_CLUSTERING,
 
4499         .eh_abort_handler               = megaraid_abort,
 
4500         .eh_device_reset_handler        = megaraid_reset,
 
4501         .eh_bus_reset_handler           = megaraid_reset,
 
4502         .eh_host_reset_handler          = megaraid_reset,
 
4505 static int __devinit
 
4506 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 
4508         struct Scsi_Host *host;
 
4510         unsigned long mega_baseport, tbase, flag = 0;
 
4511         u16 subsysid, subsysvid;
 
4512         u8 pci_bus, pci_dev_func;
 
4514         int error = -ENODEV;
 
4516         if (pci_enable_device(pdev))
 
4518         pci_set_master(pdev);
 
4520         pci_bus = pdev->bus->number;
 
4521         pci_dev_func = pdev->devfn;
 
4524          * The megaraid3 stuff reports the ID of the Intel part which is not
 
4525          * remotely specific to the megaraid
 
4527         if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
 
4530                  * Don't fall over the Compaq management cards using the same
 
4533                 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
 
4534                     pdev->subsystem_device == 0xC000)
 
4536                 /* Now check the magic signature byte */
 
4537                 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
 
4538                 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
 
4540                 /* Ok it is probably a megaraid */
 
4544          * For these vendor and device ids, signature offsets are not
 
4545          * valid and 64 bit is implicit
 
4547         if (id->driver_data & BOARD_64BIT)
 
4548                 flag |= BOARD_64BIT;
 
4552                 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
 
4553                 if (magic64 == HBA_SIGNATURE_64BIT)
 
4554                         flag |= BOARD_64BIT;
 
4557         subsysvid = pdev->subsystem_vendor;
 
4558         subsysid = pdev->subsystem_device;
 
4560         printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
 
4561                 id->vendor, id->device, pci_bus);
 
4563         printk("slot %d:func %d\n",
 
4564                 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
 
4566         /* Read the base port and IRQ from PCI */
 
4567         mega_baseport = pci_resource_start(pdev, 0);
 
4570         tbase = mega_baseport;
 
4571         if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
 
4572                 flag |= BOARD_MEMMAP;
 
4574                 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
 
4575                         printk(KERN_WARNING "megaraid: mem region busy!\n");
 
4576                         goto out_disable_device;
 
4579                 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
 
4580                 if (!mega_baseport) {
 
4582                                "megaraid: could not map hba memory\n");
 
4583                         goto out_release_region;
 
4586                 flag |= BOARD_IOMAP;
 
4587                 mega_baseport += 0x10;
 
4589                 if (!request_region(mega_baseport, 16, "megaraid"))
 
4590                         goto out_disable_device;
 
4593         /* Initialize SCSI Host structure */
 
4594         host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
 
4598         adapter = (adapter_t *)host->hostdata;
 
4599         memset(adapter, 0, sizeof(adapter_t));
 
4602                 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
 
4603                 host->host_no, mega_baseport, irq);
 
4605         adapter->base = mega_baseport;
 
4606         if (flag & BOARD_MEMMAP)
 
4607                 adapter->mmio_base = (void __iomem *) mega_baseport;
 
4609         INIT_LIST_HEAD(&adapter->free_list);
 
4610         INIT_LIST_HEAD(&adapter->pending_list);
 
4611         INIT_LIST_HEAD(&adapter->completed_list);
 
4613         adapter->flag = flag;
 
4614         spin_lock_init(&adapter->lock);
 
4616         host->cmd_per_lun = max_cmd_per_lun;
 
4617         host->max_sectors = max_sectors_per_io;
 
4619         adapter->dev = pdev;
 
4620         adapter->host = host;
 
4622         adapter->host->irq = irq;
 
4624         if (flag & BOARD_MEMMAP)
 
4625                 adapter->host->base = tbase;
 
4627                 adapter->host->io_port = tbase;
 
4628                 adapter->host->n_io_port = 16;
 
4631         adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
 
4634          * Allocate buffer to issue internal commands.
 
4636         adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
 
4637                 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
 
4638         if (!adapter->mega_buffer) {
 
4639                 printk(KERN_WARNING "megaraid: out of RAM.\n");
 
4643         adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
 
4644         if (!adapter->scb_list) {
 
4645                 printk(KERN_WARNING "megaraid: out of RAM.\n");
 
4646                 goto out_free_cmd_buffer;
 
4649         if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
 
4650                                 megaraid_isr_memmapped : megaraid_isr_iomapped,
 
4651                                         IRQF_SHARED, "megaraid", adapter)) {
 
4653                         "megaraid: Couldn't register IRQ %d!\n", irq);
 
4654                 goto out_free_scb_list;
 
4657         if (mega_setup_mailbox(adapter))
 
4660         if (mega_query_adapter(adapter))
 
4664          * Have checks for some buggy f/w
 
4666         if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
 
4670                 if (!strcmp(adapter->fw_version, "3.00") ||
 
4671                                 !strcmp(adapter->fw_version, "3.01")) {
 
4673                         printk( KERN_WARNING
 
4674                                 "megaraid: Your  card is a Dell PERC "
 
4675                                 "2/SC RAID controller with  "
 
4676                                 "firmware\nmegaraid: 3.00 or 3.01.  "
 
4677                                 "This driver is known to have "
 
4678                                 "corruption issues\nmegaraid: with "
 
4679                                 "those firmware versions on this "
 
4680                                 "specific card.  In order\nmegaraid: "
 
4681                                 "to protect your data, please upgrade "
 
4682                                 "your firmware to version\nmegaraid: "
 
4683                                 "3.10 or later, available from the "
 
4684                                 "Dell Technical Support web\n"
 
4685                                 "megaraid: site at\nhttp://support."
 
4686                                 "dell.com/us/en/filelib/download/"
 
4687                                 "index.asp?fileid=2940\n"
 
4693          * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
 
4694          * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
 
4695          * support, since this firmware cannot handle 64 bit
 
4698         if ((subsysvid == HP_SUBSYS_VID) &&
 
4699             ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
 
4703                 if (!strcmp(adapter->fw_version, "H01.07") ||
 
4704                     !strcmp(adapter->fw_version, "H01.08") ||
 
4705                     !strcmp(adapter->fw_version, "H01.09") ) {
 
4707                                 "megaraid: Firmware H.01.07, "
 
4708                                 "H.01.08, and H.01.09 on 1M/2M "
 
4710                                 "megaraid: do not support 64 bit "
 
4711                                 "addressing.\nmegaraid: DISABLING "
 
4712                                 "64 bit support.\n");
 
4713                         adapter->flag &= ~BOARD_64BIT;
 
4717         if (mega_is_bios_enabled(adapter))
 
4718                 mega_hbas[hba_count].is_bios_enabled = 1;
 
4719         mega_hbas[hba_count].hostdata_addr = adapter;
 
4722          * Find out which channel is raid and which is scsi. This is
 
4725         mega_enum_raid_scsi(adapter);
 
4728          * Find out if a logical drive is set as the boot drive. If
 
4729          * there is one, will make that as the first logical drive.
 
4730          * ROMB: Do we have to boot from a physical drive. Then all
 
4731          * the physical drives would appear before the logical disks.
 
4732          * Else, all the physical drives would be exported to the mid
 
4733          * layer after logical drives.
 
4735         mega_get_boot_drv(adapter);
 
4737         if (adapter->boot_pdrv_enabled) {
 
4738                 j = adapter->product_info.nchannels;
 
4739                 for( i = 0; i < j; i++ )
 
4740                         adapter->logdrv_chan[i] = 0;
 
4741                 for( i = j; i < NVIRT_CHAN + j; i++ )
 
4742                         adapter->logdrv_chan[i] = 1;
 
4744                 for (i = 0; i < NVIRT_CHAN; i++)
 
4745                         adapter->logdrv_chan[i] = 1;
 
4746                 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
 
4747                         adapter->logdrv_chan[i] = 0;
 
4748                 adapter->mega_ch_class <<= NVIRT_CHAN;
 
4752          * Do we support random deletion and addition of logical
 
4755         adapter->read_ldidmap = 0;      /* set it after first logdrv
 
4757         adapter->support_random_del = mega_support_random_del(adapter);
 
4759         /* Initialize SCBs */
 
4760         if (mega_init_scb(adapter))
 
4764          * Reset the pending commands counter
 
4766         atomic_set(&adapter->pend_cmds, 0);
 
4769          * Reset the adapter quiescent flag
 
4771         atomic_set(&adapter->quiescent, 0);
 
4773         hba_soft_state[hba_count] = adapter;
 
4776          * Fill in the structure which needs to be passed back to the
 
4777          * application when it does an ioctl() for controller related
 
4782         mcontroller[i].base = mega_baseport;
 
4783         mcontroller[i].irq = irq;
 
4784         mcontroller[i].numldrv = adapter->numldrv;
 
4785         mcontroller[i].pcibus = pci_bus;
 
4786         mcontroller[i].pcidev = id->device;
 
4787         mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
 
4788         mcontroller[i].pciid = -1;
 
4789         mcontroller[i].pcivendor = id->vendor;
 
4790         mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
 
4791         mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
 
4794         /* Set the Mode of addressing to 64 bit if we can */
 
4795         if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
 
4796                 pci_set_dma_mask(pdev, DMA_64BIT_MASK);
 
4797                 adapter->has_64bit_addr = 1;
 
4799                 pci_set_dma_mask(pdev, DMA_32BIT_MASK);
 
4800                 adapter->has_64bit_addr = 0;
 
4803         mutex_init(&adapter->int_mtx);
 
4804         init_completion(&adapter->int_waitq);
 
4806         adapter->this_id = DEFAULT_INITIATOR_ID;
 
4807         adapter->host->this_id = DEFAULT_INITIATOR_ID;
 
4809 #if MEGA_HAVE_CLUSTERING
 
4811          * Is cluster support enabled on this controller
 
4812          * Note: In a cluster the HBAs ( the initiators ) will have
 
4813          * different target IDs and we cannot assume it to be 7. Call
 
4814          * to mega_support_cluster() will get the target ids also if
 
4815          * the cluster support is available
 
4817         adapter->has_cluster = mega_support_cluster(adapter);
 
4818         if (adapter->has_cluster) {
 
4820                         "megaraid: Cluster driver, initiator id:%d\n",
 
4825         pci_set_drvdata(pdev, host);
 
4827         mega_create_proc_entry(hba_count, mega_proc_dir_entry);
 
4829         error = scsi_add_host(host, &pdev->dev);
 
4833         scsi_scan_host(host);
 
4838         pci_free_consistent(adapter->dev, sizeof(mbox64_t),
 
4839                         adapter->una_mbox64, adapter->una_mbox64_dma);
 
4841         free_irq(adapter->host->irq, adapter);
 
4843         kfree(adapter->scb_list);
 
4844  out_free_cmd_buffer:
 
4845         pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
 
4846                         adapter->mega_buffer, adapter->buf_dma_handle);
 
4848         scsi_host_put(host);
 
4850         if (flag & BOARD_MEMMAP)
 
4851                 iounmap((void *)mega_baseport);
 
4853         if (flag & BOARD_MEMMAP)
 
4854                 release_mem_region(tbase, 128);
 
4856                 release_region(mega_baseport, 16);
 
4858         pci_disable_device(pdev);
 
4864 __megaraid_shutdown(adapter_t *adapter)
 
4866         u_char  raw_mbox[sizeof(struct mbox_out)];
 
4867         mbox_t  *mbox = (mbox_t *)raw_mbox;
 
4870         /* Flush adapter cache */
 
4871         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
4872         raw_mbox[0] = FLUSH_ADAPTER;
 
4874         free_irq(adapter->host->irq, adapter);
 
4876         /* Issue a blocking (interrupts disabled) command to the card */
 
4877         issue_scb_block(adapter, raw_mbox);
 
4879         /* Flush disks cache */
 
4880         memset(&mbox->m_out, 0, sizeof(raw_mbox));
 
4881         raw_mbox[0] = FLUSH_SYSTEM;
 
4883         /* Issue a blocking (interrupts disabled) command to the card */
 
4884         issue_scb_block(adapter, raw_mbox);
 
4886         if (atomic_read(&adapter->pend_cmds) > 0)
 
4887                 printk(KERN_WARNING "megaraid: pending commands!!\n");
 
4890          * Have a delibrate delay to make sure all the caches are
 
4893         for (i = 0; i <= 10; i++)
 
4897 static void __devexit
 
4898 megaraid_remove_one(struct pci_dev *pdev)
 
4900         struct Scsi_Host *host = pci_get_drvdata(pdev);
 
4901         adapter_t *adapter = (adapter_t *)host->hostdata;
 
4903         scsi_remove_host(host);
 
4905         __megaraid_shutdown(adapter);
 
4907         /* Free our resources */
 
4908         if (adapter->flag & BOARD_MEMMAP) {
 
4909                 iounmap((void *)adapter->base);
 
4910                 release_mem_region(adapter->host->base, 128);
 
4912                 release_region(adapter->base, 16);
 
4914         mega_free_sgl(adapter);
 
4916 #ifdef CONFIG_PROC_FS
 
4917         if (adapter->controller_proc_dir_entry) {
 
4918                 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
 
4919                 remove_proc_entry("config",
 
4920                                 adapter->controller_proc_dir_entry);
 
4921                 remove_proc_entry("mailbox",
 
4922                                 adapter->controller_proc_dir_entry);
 
4923 #if MEGA_HAVE_ENH_PROC
 
4924                 remove_proc_entry("rebuild-rate",
 
4925                                 adapter->controller_proc_dir_entry);
 
4926                 remove_proc_entry("battery-status",
 
4927                                 adapter->controller_proc_dir_entry);
 
4929                 remove_proc_entry("diskdrives-ch0",
 
4930                                 adapter->controller_proc_dir_entry);
 
4931                 remove_proc_entry("diskdrives-ch1",
 
4932                                 adapter->controller_proc_dir_entry);
 
4933                 remove_proc_entry("diskdrives-ch2",
 
4934                                 adapter->controller_proc_dir_entry);
 
4935                 remove_proc_entry("diskdrives-ch3",
 
4936                                 adapter->controller_proc_dir_entry);
 
4938                 remove_proc_entry("raiddrives-0-9",
 
4939                                 adapter->controller_proc_dir_entry);
 
4940                 remove_proc_entry("raiddrives-10-19",
 
4941                                 adapter->controller_proc_dir_entry);
 
4942                 remove_proc_entry("raiddrives-20-29",
 
4943                                 adapter->controller_proc_dir_entry);
 
4944                 remove_proc_entry("raiddrives-30-39",
 
4945                                 adapter->controller_proc_dir_entry);
 
4948                         char    buf[12] = { 0 };
 
4949                         sprintf(buf, "hba%d", adapter->host->host_no);
 
4950                         remove_proc_entry(buf, mega_proc_dir_entry);
 
4955         pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
 
4956                         adapter->mega_buffer, adapter->buf_dma_handle);
 
4957         kfree(adapter->scb_list);
 
4958         pci_free_consistent(adapter->dev, sizeof(mbox64_t),
 
4959                         adapter->una_mbox64, adapter->una_mbox64_dma);
 
4961         scsi_host_put(host);
 
4962         pci_disable_device(pdev);
 
4968 megaraid_shutdown(struct pci_dev *pdev)
 
4970         struct Scsi_Host *host = pci_get_drvdata(pdev);
 
4971         adapter_t *adapter = (adapter_t *)host->hostdata;
 
4973         __megaraid_shutdown(adapter);
 
4976 static struct pci_device_id megaraid_pci_tbl[] = {
 
4977         {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
 
4978                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 
4979         {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
 
4980                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 
4981         {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
 
4982                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 
4985 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
 
4987 static struct pci_driver megaraid_pci_driver = {
 
4988         .name           = "megaraid_legacy",
 
4989         .id_table       = megaraid_pci_tbl,
 
4990         .probe          = megaraid_probe_one,
 
4991         .remove         = __devexit_p(megaraid_remove_one),
 
4992         .shutdown       = megaraid_shutdown,
 
4995 static int __init megaraid_init(void)
 
4999         if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
 
5000                 max_cmd_per_lun = MAX_CMD_PER_LUN;
 
5001         if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
 
5002                 max_mbox_busy_wait = MBOX_BUSY_WAIT;
 
5004 #ifdef CONFIG_PROC_FS
 
5005         mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
 
5006         if (!mega_proc_dir_entry) {
 
5008                                 "megaraid: failed to create megaraid root\n");
 
5011         error = pci_register_driver(&megaraid_pci_driver);
 
5013 #ifdef CONFIG_PROC_FS
 
5014                 remove_proc_entry("megaraid", NULL);
 
5020          * Register the driver as a character device, for applications
 
5021          * to access it for ioctls.
 
5022          * First argument (major) to register_chrdev implies a dynamic
 
5023          * major number allocation.
 
5025         major = register_chrdev(0, "megadev_legacy", &megadev_fops);
 
5028                                 "megaraid: failed to register char device\n");
 
5034 static void __exit megaraid_exit(void)
 
5037          * Unregister the character device interface to the driver.
 
5039         unregister_chrdev(major, "megadev_legacy");
 
5041         pci_unregister_driver(&megaraid_pci_driver);
 
5043 #ifdef CONFIG_PROC_FS
 
5044         remove_proc_entry("megaraid", NULL);
 
5048 module_init(megaraid_init);
 
5049 module_exit(megaraid_exit);
 
5051 /* vi: set ts=8 sw=8 tw=78: */