blob: 9aa048525eb2544b8159a9495f1a604695a79d26 [file] [log] [blame]
/*
*
* Linux MegaRAID device driver
*
* Copyright (c) 2002 LSI Logic Corporation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Copyright (c) 2002 Red Hat, Inc. All rights reserved.
* - fixes
* - speed-ups (list handling fixes, issued_list, optimizations.)
* - lots of cleanups.
*
* Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
* - new-style, hotplug-aware pci probing and scsi registration
*
* Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
* <Seokmann.Ju@lsil.com>
*
* Description: Linux device driver for LSI Logic MegaRAID controller
*
* Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
* 518, 520, 531, 532
*
* This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
* and others. Please send updates to the mailing list
* linux-scsi@vger.kernel.org .
*
*/
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/reboot.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <scsi/scsicam.h>
#include "scsi.h"
#include <scsi/scsi_host.h>
#include "megaraid.h"
#define MEGARAID_MODULE_VERSION "2.00.4"
MODULE_AUTHOR ("sju@lsil.com");
MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
MODULE_LICENSE ("GPL");
MODULE_VERSION(MEGARAID_MODULE_VERSION);
static DEFINE_MUTEX(megadev_mutex);
static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
module_param(max_cmd_per_lun, uint, 0);
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)");
static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
module_param(max_sectors_per_io, ushort, 0);
MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
module_param(max_mbox_busy_wait, ushort, 0);
MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
#define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
#define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
#define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
#define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
/*
* Global variables
*/
static int hba_count;
static adapter_t *hba_soft_state[MAX_CONTROLLERS];
static struct proc_dir_entry *mega_proc_dir_entry;
/* For controller re-ordering */
static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
static long
megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
/*
* The File Operations structure for the serial/ioctl interface of the driver
*/
static const struct file_operations megadev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = megadev_unlocked_ioctl,
.open = megadev_open,
.llseek = noop_llseek,
};
/*
* Array to structures for storing the information about the controllers. This
* information is sent to the user level applications, when they do an ioctl
* for this information.
*/
static struct mcontroller mcontroller[MAX_CONTROLLERS];
/* The current driver version */
static u32 driver_ver = 0x02000000;
/* major number used by the device for character interface */
static int major;
#define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
/*
* Debug variable to print some diagnostic messages
*/
static int trace_level;
/**
* mega_setup_mailbox()
* @adapter - pointer to our soft state
*
* Allocates a 8 byte aligned memory for the handshake mailbox.
*/
static int
mega_setup_mailbox(adapter_t *adapter)
{
unsigned long align;
adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
sizeof(mbox64_t), &adapter->una_mbox64_dma);
if( !adapter->una_mbox64 ) return -1;
adapter->mbox = &adapter->una_mbox64->mbox;
adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
(~0UL ^ 0xFUL));
adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
/*
* Register the mailbox if the controller is an io-mapped controller
*/
if( adapter->flag & BOARD_IOMAP ) {
outb(adapter->mbox_dma & 0xFF,
adapter->host->io_port + MBOX_PORT0);
outb((adapter->mbox_dma >> 8) & 0xFF,
adapter->host->io_port + MBOX_PORT1);
outb((adapter->mbox_dma >> 16) & 0xFF,
adapter->host->io_port + MBOX_PORT2);
outb((adapter->mbox_dma >> 24) & 0xFF,
adapter->host->io_port + MBOX_PORT3);
outb(ENABLE_MBOX_BYTE,
adapter->host->io_port + ENABLE_MBOX_REGION);
irq_ack(adapter);
irq_enable(adapter);
}
return 0;
}
/*
* mega_query_adapter()
* @adapter - pointer to our soft state
*
* Issue the adapter inquiry commands to the controller and find out
* information and parameter about the devices attached
*/
static int
mega_query_adapter(adapter_t *adapter)
{
dma_addr_t prod_info_dma_handle;
mega_inquiry3 *inquiry3;
u8 raw_mbox[sizeof(struct mbox_out)];
mbox_t *mbox;
int retval;
/* Initialize adapter inquiry mailbox */
mbox = (mbox_t *)raw_mbox;
memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
memset(&mbox->m_out, 0, sizeof(raw_mbox));
/*
* Try to issue Inquiry3 command
* if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
* update enquiry3 structure
*/
mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
/* Issue a blocking command to the card */
if ((retval = issue_scb_block(adapter, raw_mbox))) {
/* the adapter does not support 40ld */
mraid_ext_inquiry *ext_inq;
mraid_inquiry *inq;
dma_addr_t dma_handle;
ext_inq = pci_alloc_consistent(adapter->dev,
sizeof(mraid_ext_inquiry), &dma_handle);
if( ext_inq == NULL ) return -1;
inq = &ext_inq->raid_inq;
mbox->m_out.xferaddr = (u32)dma_handle;
/*issue old 0x04 command to adapter */
mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
issue_scb_block(adapter, raw_mbox);
/*
* update Enquiry3 and ProductInfo structures with
* mraid_inquiry structure
*/
mega_8_to_40ld(inq, inquiry3,
(mega_product_info *)&adapter->product_info);
pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
ext_inq, dma_handle);
} else { /*adapter supports 40ld */
adapter->flag |= BOARD_40LD;
/*
* get product_info, which is static information and will be
* unchanged
*/
prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
&adapter->product_info,
sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
mbox->m_out.xferaddr = prod_info_dma_handle;
raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
if ((retval = issue_scb_block(adapter, raw_mbox)))
printk(KERN_WARNING
"megaraid: Product_info cmd failed with error: %d\n",
retval);
pci_unmap_single(adapter->dev, prod_info_dma_handle,
sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
}
/*
* kernel scans the channels from 0 to <= max_channel
*/
adapter->host->max_channel =
adapter->product_info.nchannels + NVIRT_CHAN -1;
adapter->host->max_id = 16; /* max targets per channel */
adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
adapter->host->cmd_per_lun = max_cmd_per_lun;
adapter->numldrv = inquiry3->num_ldrv;
adapter->max_cmds = adapter->product_info.max_commands;
if(adapter->max_cmds > MAX_COMMANDS)
adapter->max_cmds = MAX_COMMANDS;
adapter->host->can_queue = adapter->max_cmds - 1;
/*
* Get the maximum number of scatter-gather elements supported by this
* firmware
*/
mega_get_max_sgl(adapter);
adapter->host->sg_tablesize = adapter->sglen;
/* use HP firmware and bios version encoding */
if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
sprintf (adapter->fw_version, "%c%d%d.%d%d",
adapter->product_info.fw_version[2],
adapter->product_info.fw_version[1] >> 8,
adapter->product_info.fw_version[1] & 0x0f,
adapter->product_info.fw_version[0] >> 8,
adapter->product_info.fw_version[0] & 0x0f);
sprintf (adapter->bios_version, "%c%d%d.%d%d",
adapter->product_info.bios_version[2],
adapter->product_info.bios_version[1] >> 8,
adapter->product_info.bios_version[1] & 0x0f,
adapter->product_info.bios_version[0] >> 8,
adapter->product_info.bios_version[0] & 0x0f);
} else {
memcpy(adapter->fw_version,
(char *)adapter->product_info.fw_version, 4);
adapter->fw_version[4] = 0;
memcpy(adapter->bios_version,
(char *)adapter->product_info.bios_version, 4);
adapter->bios_version[4] = 0;
}
printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
adapter->fw_version, adapter->bios_version, adapter->numldrv);
/*
* Do we support extended (>10 bytes) cdbs
*/
adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
if (adapter->support_ext_cdb)
printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
return 0;
}
/**
* mega_runpendq()
* @adapter - pointer to our soft state
*
* Runs through the list of pending requests.
*/
static inline void
mega_runpendq(adapter_t *adapter)
{
if(!list_empty(&adapter->pending_list))
__mega_runpendq(adapter);
}
/*
* megaraid_queue()
* @scmd - Issue this scsi command
* @done - the callback hook into the scsi mid-layer
*
* The command queuing entry point for the mid-layer.
*/
static int
megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
{
adapter_t *adapter;
scb_t *scb;
int busy=0;
unsigned long flags;
adapter = (adapter_t *)scmd->device->host->hostdata;
scmd->scsi_done = done;
/*
* Allocate and build a SCB request
* busy flag will be set if mega_build_cmd() command could not
* allocate scb. We will return non-zero status in that case.
* NOTE: scb can be null even though certain commands completed
* successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
* return 0 in that case.
*/
spin_lock_irqsave(&adapter->lock, flags);
scb = mega_build_cmd(adapter, scmd, &busy);
if (!scb)
goto out;
scb->state |= SCB_PENDQ;
list_add_tail(&scb->list, &adapter->pending_list);
/*
* Check if the HBA is in quiescent state, e.g., during a
* delete logical drive opertion. If it is, don't run
* the pending_list.
*/
if (atomic_read(&adapter->quiescent) == 0)
mega_runpendq(adapter);
busy = 0;
out:
spin_unlock_irqrestore(&adapter->lock, flags);
return busy;
}
static DEF_SCSI_QCMD(megaraid_queue)
/**
* mega_allocate_scb()
* @adapter - pointer to our soft state
* @cmd - scsi command from the mid-layer
*
* Allocate a SCB structure. This is the central structure for controller
* commands.
*/
static inline scb_t *
mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
{
struct list_head *head = &adapter->free_list;
scb_t *scb;
/* Unlink command from Free List */
if( !list_empty(head) ) {
scb = list_entry(head->next, scb_t, list);
list_del_init(head->next);
scb->state = SCB_ACTIVE;
scb->cmd = cmd;
scb->dma_type = MEGA_DMA_TYPE_NONE;
return scb;
}
return NULL;
}
/**
* mega_get_ldrv_num()
* @adapter - pointer to our soft state
* @cmd - scsi mid layer command
* @channel - channel on the controller
*
* Calculate the logical drive number based on the information in scsi command
* and the channel number.
*/
static inline int
mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
{
int tgt;
int ldrv_num;
tgt = cmd->device->id;
if ( tgt > adapter->this_id )
tgt--; /* we do not get inquires for initiator id */
ldrv_num = (channel * 15) + tgt;
/*
* If we have a logical drive with boot enabled, project it first
*/
if( adapter->boot_ldrv_enabled ) {
if( ldrv_num == 0 ) {
ldrv_num = adapter->boot_ldrv;
}
else {
if( ldrv_num <= adapter->boot_ldrv ) {
ldrv_num--;
}
}
}
/*
* If "delete logical drive" feature is enabled on this controller.
* Do only if at least one delete logical drive operation was done.
*
* Also, after logical drive deletion, instead of logical drive number,
* the value returned should be 0x80+logical drive id.
*
* These is valid only for IO commands.
*/
if (adapter->support_random_del && adapter->read_ldidmap )
switch (cmd->cmnd[0]) {
case READ_6: /* fall through */
case WRITE_6: /* fall through */
case READ_10: /* fall through */
case WRITE_10:
ldrv_num += 0x80;
}
return ldrv_num;
}
/**
* mega_build_cmd()
* @adapter - pointer to our soft state
* @cmd - Prepare using this scsi command
* @busy - busy flag if no resources
*
* Prepares a command and scatter gather list for the controller. This routine
* also finds out if the commands is intended for a logical drive or a
* physical device and prepares the controller command accordingly.
*
* We also re-order the logical drives and physical devices based on their
* boot settings.
*/
static scb_t *
mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
{
mega_ext_passthru *epthru;
mega_passthru *pthru;
scb_t *scb;
mbox_t *mbox;
long seg;
char islogical;
int max_ldrv_num;
int channel = 0;
int target = 0;
int ldrv_num = 0; /* logical drive number */
/*
* filter the internal and ioctl commands
*/
if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
return (scb_t *)cmd->host_scribble;
/*
* We know what channels our logical drives are on - mega_find_card()
*/
islogical = adapter->logdrv_chan[cmd->device->channel];
/*
* The theory: If physical drive is chosen for boot, all the physical
* devices are exported before the logical drives, otherwise physical
* devices are pushed after logical drives, in which case - Kernel sees
* the physical devices on virtual channel which is obviously converted
* to actual channel on the HBA.
*/
if( adapter->boot_pdrv_enabled ) {
if( islogical ) {
/* logical channel */
channel = cmd->device->channel -
adapter->product_info.nchannels;
}
else {
/* this is physical channel */
channel = cmd->device->channel;
target = cmd->device->id;
/*
* boot from a physical disk, that disk needs to be
* exposed first IF both the channels are SCSI, then
* booting from the second channel is not allowed.
*/
if( target == 0 ) {
target = adapter->boot_pdrv_tgt;
}
else if( target == adapter->boot_pdrv_tgt ) {
target = 0;
}
}
}
else {
if( islogical ) {
/* this is the logical channel */
channel = cmd->device->channel;
}
else {
/* physical channel */
channel = cmd->device->channel - NVIRT_CHAN;
target = cmd->device->id;
}
}
if(islogical) {
/* have just LUN 0 for each target on virtual channels */
if (cmd->device->lun) {
cmd->result = (DID_BAD_TARGET << 16);
cmd->scsi_done(cmd);
return NULL;
}
ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
max_ldrv_num = (adapter->flag & BOARD_40LD) ?
MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
/*
* max_ldrv_num increases by 0x80 if some logical drive was
* deleted.
*/
if(adapter->read_ldidmap)
max_ldrv_num += 0x80;
if(ldrv_num > max_ldrv_num ) {
cmd->result = (DID_BAD_TARGET << 16);
cmd->scsi_done(cmd);
return NULL;
}
}
else {
if( cmd->device->lun > 7) {
/*
* Do not support lun >7 for physically accessed
* devices
*/
cmd->result = (DID_BAD_TARGET << 16);
cmd->scsi_done(cmd);
return NULL;
}
}
/*
*
* Logical drive commands
*
*/
if(islogical) {
switch (cmd->cmnd[0]) {
case TEST_UNIT_READY:
#if MEGA_HAVE_CLUSTERING
/*
* Do we support clustering and is the support enabled
* If no, return success always
*/
if( !adapter->has_cluster ) {
cmd->result = (DID_OK << 16);
cmd->scsi_done(cmd);
return NULL;
}
if(!(scb = mega_allocate_scb(adapter, cmd))) {
*busy = 1;
return NULL;
}
scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
scb->raw_mbox[3] = ldrv_num;
scb->dma_direction = PCI_DMA_NONE;
return scb;
#else
cmd->result = (DID_OK << 16);
cmd->scsi_done(cmd);
return NULL;
#endif
case MODE_SENSE: {
char *buf;
struct scatterlist *sg;
sg = scsi_sglist(cmd);
buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
memset(buf, 0, cmd->cmnd[4]);
kunmap_atomic(buf - sg->offset, KM_IRQ0);
cmd->result = (DID_OK << 16);
cmd->scsi_done(cmd);
return NULL;
}
case READ_CAPACITY:
case INQUIRY:
if(!(adapter->flag & (1L << cmd->device->channel))) {
printk(KERN_NOTICE
"scsi%d: scanning scsi channel %d ",
adapter->host->host_no,
cmd->device->channel);
printk("for logical drives.\n");
adapter->flag |= (1L << cmd->device->channel);
}
/* Allocate a SCB and initialize passthru */
if(!(scb = mega_allocate_scb(adapter, cmd))) {
*busy = 1;
return NULL;
}
pthru = scb->pthru;
mbox = (mbox_t *)scb->raw_mbox;
memset(mbox, 0, sizeof(scb->raw_mbox));
memset(pthru, 0, sizeof(mega_passthru));
pthru->timeout = 0;
pthru->ars = 1;
pthru->reqsenselen = 14;
pthru->islogical = 1;
pthru->logdrv = ldrv_num;
pthru->cdblen = cmd->cmd_len;
memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
if( adapter->has_64bit_addr ) {
mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
}
else {
mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
}
scb->dma_direction = PCI_DMA_FROMDEVICE;
pthru->numsgelements = mega_build_sglist(adapter, scb,
&pthru->dataxferaddr, &pthru->dataxferlen);
mbox->m_out.xferaddr = scb->pthru_dma_addr;
return scb;
case READ_6:
case WRITE_6:
case READ_10:
case WRITE_10:
case READ_12:
case WRITE_12:
/* Allocate a SCB and initialize mailbox */
if(!(scb = mega_allocate_scb(adapter, cmd))) {
*busy = 1;
return NULL;
}
mbox = (mbox_t *)scb->raw_mbox;
memset(mbox, 0, sizeof(scb->raw_mbox));
mbox->m_out.logdrv = ldrv_num;
/*
* A little hack: 2nd bit is zero for all scsi read
* commands and is set for all scsi write commands
*/
if( adapter->has_64bit_addr ) {
mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
MEGA_MBOXCMD_LWRITE64:
MEGA_MBOXCMD_LREAD64 ;
}
else {
mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
MEGA_MBOXCMD_LWRITE:
MEGA_MBOXCMD_LREAD ;
}
/*
* 6-byte READ(0x08) or WRITE(0x0A) cdb
*/
if( cmd->cmd_len == 6 ) {
mbox->m_out.numsectors = (u32) cmd->cmnd[4];
mbox->m_out.lba =
((u32)cmd->cmnd[1] << 16) |
((u32)cmd->cmnd[2] << 8) |
(u32)cmd->cmnd[3];
mbox->m_out.lba &= 0x1FFFFF;
#if MEGA_HAVE_STATS
/*
* Take modulo 0x80, since the logical drive
* number increases by 0x80 when a logical
* drive was deleted
*/
if (*cmd->cmnd == READ_6) {
adapter->nreads[ldrv_num%0x80]++;
adapter->nreadblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
} else {
adapter->nwrites[ldrv_num%0x80]++;
adapter->nwriteblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
}
#endif
}
/*
* 10-byte READ(0x28) or WRITE(0x2A) cdb
*/
if( cmd->cmd_len == 10 ) {
mbox->m_out.numsectors =
(u32)cmd->cmnd[8] |
((u32)cmd->cmnd[7] << 8);
mbox->m_out.lba =
((u32)cmd->cmnd[2] << 24) |
((u32)cmd->cmnd[3] << 16) |
((u32)cmd->cmnd[4] << 8) |
(u32)cmd->cmnd[5];
#if MEGA_HAVE_STATS
if (*cmd->cmnd == READ_10) {
adapter->nreads[ldrv_num%0x80]++;
adapter->nreadblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
} else {
adapter->nwrites[ldrv_num%0x80]++;
adapter->nwriteblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
}
#endif
}
/*
* 12-byte READ(0xA8) or WRITE(0xAA) cdb
*/
if( cmd->cmd_len == 12 ) {
mbox->m_out.lba =
((u32)cmd->cmnd[2] << 24) |
((u32)cmd->cmnd[3] << 16) |
((u32)cmd->cmnd[4] << 8) |
(u32)cmd->cmnd[5];
mbox->m_out.numsectors =
((u32)cmd->cmnd[6] << 24) |
((u32)cmd->cmnd[7] << 16) |
((u32)cmd->cmnd[8] << 8) |
(u32)cmd->cmnd[9];
#if MEGA_HAVE_STATS
if (*cmd->cmnd == READ_12) {
adapter->nreads[ldrv_num%0x80]++;
adapter->nreadblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
} else {
adapter->nwrites[ldrv_num%0x80]++;
adapter->nwriteblocks[ldrv_num%0x80] +=
mbox->m_out.numsectors;
}
#endif
}
/*
* If it is a read command
*/
if( (*cmd->cmnd & 0x0F) == 0x08 ) {
scb->dma_direction = PCI_DMA_FROMDEVICE;
}
else {
scb->dma_direction = PCI_DMA_TODEVICE;
}
/* Calculate Scatter-Gather info */
mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
(u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
return scb;
#if MEGA_HAVE_CLUSTERING
case RESERVE: /* Fall through */
case RELEASE:
/*
* Do we support clustering and is the support enabled
*/
if( ! adapter->has_cluster ) {
cmd->result = (DID_BAD_TARGET << 16);
cmd->scsi_done(cmd);
return NULL;
}
/* Allocate a SCB and initialize mailbox */
if(!(scb = mega_allocate_scb(adapter, cmd))) {
*busy = 1;
return NULL;
}
scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
MEGA_RESERVE_LD : MEGA_RELEASE_LD;
scb->raw_mbox[3] = ldrv_num;
scb->dma_direction = PCI_DMA_NONE;
return scb;
#endif
default:
cmd->result = (DID_BAD_TARGET << 16);
cmd->scsi_done(cmd);
return NULL;
}
}
/*
* Passthru drive commands
*/
else {
/* Allocate a SCB and initialize passthru */
if(!(scb = mega_allocate_scb(adapter, cmd))) {
*busy = 1;
return NULL;
}
mbox = (mbox_t *)scb->raw_mbox;
memset(mbox, 0, sizeof(scb->raw_mbox));
if( adapter->support_ext_cdb ) {
epthru = mega_prepare_extpassthru(adapter, scb, cmd,
channel, target);
mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
mbox->m_out.xferaddr = scb->epthru_dma_addr;
}
else {
pthru = mega_prepare_passthru(adapter, scb, cmd,
channel, target);
/* Initialize mailbox */
if( adapter->has_64bit_addr ) {
mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
}
else {
mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
}
mbox->m_out.xferaddr = scb->pthru_dma_addr;
}
return scb;
}
return NULL;
}
/**
* mega_prepare_passthru()
* @adapter - pointer to our soft state
* @scb - our scsi control block
* @cmd - scsi command from the mid-layer
* @channel - actual channel on the controller
* @target - actual id on the controller.
*
* prepare a command for the scsi physical devices.
*/
static mega_passthru *
mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
int channel, int target)
{
mega_passthru *pthru;
pthru = scb->pthru;
memset(pthru, 0, sizeof (mega_passthru));
/* 0=6sec/1=60sec/2=10min/3=3hrs */
pthru->timeout = 2;
pthru->ars = 1;
pthru->reqsenselen = 14;
pthru->islogical = 0;
pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
pthru->target = (adapter->flag & BOARD_40LD) ?
(channel << 4) | target : target;
pthru->cdblen = cmd->cmd_len;
pthru->logdrv = cmd->device->lun;
memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
/* Not sure about the direction */
scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
/* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
switch (cmd->cmnd[0]) {
case INQUIRY:
case READ_CAPACITY:
if(!(adapter->flag & (1L << cmd->device->channel))) {
printk(KERN_NOTICE
"scsi%d: scanning scsi channel %d [P%d] ",
adapter->host->host_no,
cmd->device->channel, channel);
printk("for physical devices.\n");
adapter->flag |= (1L << cmd->device->channel);
}
/* Fall through */
default:
pthru->numsgelements = mega_build_sglist(adapter, scb,
&pthru->dataxferaddr, &pthru->dataxferlen);
break;
}
return pthru;
}
/**
* mega_prepare_extpassthru()
* @adapter - pointer to our soft state
* @scb - our scsi control block
* @cmd - scsi command from the mid-layer
* @channel - actual channel on the controller
* @target - actual id on the controller.
*
* prepare a command for the scsi physical devices. This rountine prepares
* commands for devices which can take extended CDBs (>10 bytes)
*/
static mega_ext_passthru *
mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
int channel, int target)
{
mega_ext_passthru *epthru;
epthru = scb->epthru;
memset(epthru, 0, sizeof(mega_ext_passthru));
/* 0=6sec/1=60sec/2=10min/3=3hrs */
epthru->timeout = 2;
epthru->ars = 1;
epthru->reqsenselen = 14;
epthru->islogical = 0;
epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
epthru->target = (adapter->flag & BOARD_40LD) ?
(channel << 4) | target : target;
epthru->cdblen = cmd->cmd_len;
epthru->logdrv = cmd->device->lun;
memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
/* Not sure about the direction */
scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
switch(cmd->cmnd[0]) {
case INQUIRY:
case READ_CAPACITY:
if(!(adapter->flag & (1L << cmd->device->channel))) {
printk(KERN_NOTICE
"scsi%d: scanning scsi channel %d [P%d] ",
adapter->host->host_no,
cmd->device->channel, channel);
printk("for physical devices.\n");
adapter->flag |= (1L << cmd->device->channel);
}
/* Fall through */
default:
epthru->numsgelements = mega_build_sglist(adapter, scb,
&epthru->dataxferaddr, &epthru->dataxferlen);
break;
}
return epthru;
}
static void
__mega_runpendq(adapter_t *adapter)
{
scb_t *scb;
struct list_head *pos, *next;
/* Issue any pending commands to the card */
list_for_each_safe(pos, next, &adapter->pending_list) {
scb = list_entry(pos, scb_t, list);
if( !(scb->state & SCB_ISSUED) ) {
if( issue_scb(adapter, scb) != 0 )
return;
}
}
return;
}
/**
* issue_scb()
* @adapter - pointer to our soft state
* @scb - scsi control block
*
* Post a command to the card if the mailbox is available, otherwise return
* busy. We also take the scb from the pending list if the mailbox is
* available.
*/
static int
issue_scb(adapter_t *adapter, scb_t *scb)
{
volatile mbox64_t *mbox64 = adapter->mbox64;
volatile mbox_t *mbox = adapter->mbox;
unsigned int i = 0;
if(unlikely(mbox->m_in.busy)) {
do {
udelay(1);
i++;
} while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
if(mbox->m_in.busy) return -1;
}
/* Copy mailbox data into host structure */
memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
sizeof(struct mbox_out));
mbox->m_out.cmdid = scb->idx; /* Set cmdid */
mbox->m_in.busy = 1; /* Set busy */
/*
* Increment the pending queue counter
*/
atomic_inc(&adapter->pend_cmds);
switch (mbox->m_out.cmd) {
case MEGA_MBOXCMD_LREAD64:
case MEGA_MBOXCMD_LWRITE64:
case MEGA_MBOXCMD_PASSTHRU64:
case MEGA_MBOXCMD_EXTPTHRU:
mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
mbox64->xfer_segment_hi = 0;
mbox->m_out.xferaddr = 0xFFFFFFFF;
break;
default:
mbox64->xfer_segment_lo = 0;
mbox64->xfer_segment_hi = 0;
}
/*
* post the command
*/
scb->state |= SCB_ISSUED;
if( likely(adapter->flag & BOARD_MEMMAP) ) {
mbox->m_in.poll = 0;
mbox->m_in.ack = 0;
WRINDOOR(adapter, adapter->mbox_dma | 0x1);
}
else {
irq_enable(adapter);
issue_command(adapter);
}
return 0;
}
/*
* Wait until the controller's mailbox is available
*/
static inline int
mega_busywait_mbox (adapter_t *adapter)
{
if (adapter->mbox->m_in.busy)
return __mega_busywait_mbox(adapter);
return 0;
}
/**
* issue_scb_block()
* @adapter - pointer to our soft state
* @raw_mbox - the mailbox
*
* Issue a scb in synchronous and non-interrupt mode
*/
static int
issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
{
volatile mbox64_t *mbox64 = adapter->mbox64;
volatile mbox_t *mbox = adapter->mbox;
u8 byte;
/* Wait until mailbox is free */
if(mega_busywait_mbox (adapter))
goto bug_blocked_mailbox;
/* Copy mailbox data into host structure */
memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
mbox->m_out.cmdid = 0xFE;
mbox->m_in.busy = 1;
switch (raw_mbox[0]) {
case MEGA_MBOXCMD_LREAD64:
case MEGA_MBOXCMD_LWRITE64:
case MEGA_MBOXCMD_PASSTHRU64:
case MEGA_MBOXCMD_EXTPTHRU:
mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
mbox64->xfer_segment_hi = 0;
mbox->m_out.xferaddr = 0xFFFFFFFF;
break;
default:
mbox64->xfer_segment_lo = 0;
mbox64->xfer_segment_hi = 0;
}
if( likely(adapter->flag & BOARD_MEMMAP) ) {
mbox->m_in.poll = 0;
mbox->m_in.ack = 0;
mbox->m_in.numstatus = 0xFF;
mbox->m_in.status = 0xFF;
WRINDOOR(adapter, adapter->mbox_dma | 0x1);
while((volatile u8)mbox->m_in.numstatus == 0xFF)
cpu_relax();
mbox->m_in.numstatus = 0xFF;
while( (volatile u8)mbox->m_in.poll != 0x77 )
cpu_relax();
mbox->m_in.poll = 0;
mbox->m_in.ack = 0x77;
WRINDOOR(adapter, adapter->mbox_dma | 0x2);
while(RDINDOOR(adapter) & 0x2)
cpu_relax();
}
else {
irq_disable(adapter);
issue_command(adapter);
while (!((byte = irq_state(adapter)) & INTR_VALID))
cpu_relax();
set_irq_state(adapter, byte);
irq_enable(adapter);
irq_ack(adapter);
}
return mbox->m_in.status;
bug_blocked_mailbox:
printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
udelay (1000);
return -1;
}
/**
* megaraid_isr_iomapped()
* @irq - irq
* @devp - pointer to our soft state
*
* Interrupt service routine for io-mapped controllers.
* Find out if our device is interrupting. If yes, acknowledge the interrupt
* and service the completed commands.
*/
static irqreturn_t
megaraid_isr_iomapped(int irq, void *devp)
{
adapter_t *adapter = devp;
unsigned long flags;
u8 status;
u8 nstatus;
u8 completed[MAX_FIRMWARE_STATUS];
u8 byte;
int handled = 0;
/*
* loop till F/W has more commands for us to complete.
*/
spin_lock_irqsave(&adapter->lock, flags);
do {
/* Check if a valid interrupt is pending */
byte = irq_state(adapter);
if( (byte & VALID_INTR_BYTE) == 0 ) {
/*
* No more pending commands
*/
goto out_unlock;
}
set_irq_state(adapter, byte);
while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
== 0xFF)
cpu_relax();
adapter->mbox->m_in.numstatus = 0xFF;
status = adapter->mbox->m_in.status;
/*
* decrement the pending queue counter
*/
atomic_sub(nstatus, &adapter->pend_cmds);
memcpy(completed, (void *)adapter->mbox->m_in.completed,
nstatus);
/* Acknowledge interrupt */
irq_ack(adapter);
mega_cmd_done(adapter, completed, nstatus, status);
mega_rundoneq(adapter);
handled = 1;
/* Loop through any pending requests */
if(atomic_read(&adapter->quiescent) == 0) {
mega_runpendq(adapter);
}
} while(1);
out_unlock:
spin_unlock_irqrestore(&adapter->lock, flags);
return IRQ_RETVAL(handled);
}
/**
* megaraid_isr_memmapped()
* @irq - irq
* @devp - pointer to our soft state
*
* Interrupt service routine for memory-mapped controllers.
* Find out if our device is interrupting. If yes, acknowledge the interrupt
* and service the completed commands.
*/
static irqreturn_t
megaraid_isr_memmapped(int irq, void *devp)
{
adapter_t *adapter = devp;
unsigned long flags;
u8 status;
u32 dword = 0;
u8 nstatus;
u8 completed[MAX_FIRMWARE_STATUS];
int handled = 0;
/*
* loop till F/W has more commands for us to complete.
*/
spin_lock_irqsave(&adapter->lock, flags);
do {
/* Check if a valid interrupt is pending */
dword = RDOUTDOOR(adapter);
if(dword != 0x10001234) {
/*
* No more pending commands
*/
goto out_unlock;
}
WROUTDOOR(adapter, 0x10001234);
while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
== 0xFF) {
cpu_relax();
}
adapter->mbox->m_in.numstatus = 0xFF;
status = adapter->mbox->m_in.status;
/*
* decrement the pending queue counter
*/
atomic_sub(nstatus, &adapter->pend_cmds);
memcpy(completed, (void *)adapter->mbox->m_in.completed,
nstatus);
/* Acknowledge interrupt */
WRINDOOR(adapter, 0x2);
handled = 1;
while( RDINDOOR(adapter) & 0x02 )
cpu_relax();
mega_cmd_done(adapter, completed, nstatus, status);
mega_rundoneq(adapter);
/* Loop through any pending requests */
if(atomic_read(&adapter->quiescent) == 0) {
mega_runpendq(adapter);
}
} while(1);
out_unlock:
spin_unlock_irqrestore(&adapter->lock, flags);
return IRQ_RETVAL(handled);
}
/**
* mega_cmd_done()
* @adapter - pointer to our soft state
* @completed - array of ids of completed commands
* @nstatus - number of completed commands
* @status - status of the last command completed
*
* Complete the comamnds and call the scsi mid-layer callback hooks.
*/
static void
mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
{
mega_ext_passthru *epthru = NULL;
struct scatterlist *sgl;
Scsi_Cmnd *cmd = NULL;
mega_passthru *pthru = NULL;
mbox_t *mbox = NULL;
u8 c;
scb_t *scb;
int islogical;
int cmdid;
int i;
/*
* for all the commands completed, call the mid-layer callback routine
* and free the scb.
*/
for( i = 0; i < nstatus; i++ ) {
cmdid = completed[i];
if( cmdid == CMDID_INT_CMDS ) { /* internal command */
scb = &adapter->int_scb;
cmd = scb->cmd;
mbox = (mbox_t *)scb->raw_mbox;
/*
* Internal command interface do not fire the extended
* passthru or 64-bit passthru
*/
pthru = scb->pthru;
}
else {
scb = &adapter->scb_list[cmdid];
/*
* Make sure f/w has completed a valid command
*/
if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
printk(KERN_CRIT
"megaraid: invalid command ");
printk("Id %d, scb->state:%x, scsi cmd:%p\n",
cmdid, scb->state, scb->cmd);
continue;
}
/*
* Was a abort issued for this command
*/
if( scb->state & SCB_ABORT ) {
printk(KERN_WARNING
"megaraid: aborted cmd %lx[%x] complete.\n",
scb->cmd->serial_number, scb->idx);
scb->cmd->result = (DID_ABORT << 16);
list_add_tail(SCSI_LIST(scb->cmd),
&adapter->completed_list);
mega_free_scb(adapter, scb);
continue;
}
/*
* Was a reset issued for this command
*/
if( scb->state & SCB_RESET ) {
printk(KERN_WARNING
"megaraid: reset cmd %lx[%x] complete.\n",
scb->cmd->serial_number, scb->idx);
scb->cmd->result = (DID_RESET << 16);
list_add_tail(SCSI_LIST(scb->cmd),
&adapter->completed_list);
mega_free_scb (adapter, scb);
continue;
}
cmd = scb->cmd;
pthru = scb->pthru;
epthru = scb->epthru;
mbox = (mbox_t *)scb->raw_mbox;
#if MEGA_HAVE_STATS
{
int logdrv = mbox->m_out.logdrv;
islogical = adapter->logdrv_chan[cmd->channel];
/*
* Maintain an error counter for the logical drive.
* Some application like SNMP agent need such
* statistics
*/
if( status && islogical && (cmd->cmnd[0] == READ_6 ||
cmd->cmnd[0] == READ_10 ||
cmd->cmnd[0] == READ_12)) {
/*
* Logical drive number increases by 0x80 when
* a logical drive is deleted
*/
adapter->rd_errors[logdrv%0x80]++;
}
if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
cmd->cmnd[0] == WRITE_10 ||
cmd->cmnd[0] == WRITE_12)) {
/*
* Logical drive number increases by 0x80 when
* a logical drive is deleted
*/
adapter->wr_errors[logdrv%0x80]++;
}
}
#endif
}
/*
* Do not return the presence of hard disk on the channel so,
* inquiry sent, and returned data==hard disk or removable
* hard disk and not logical, request should return failure! -
* PJ
*/
islogical = adapter->logdrv_chan[cmd->device->channel];
if( cmd->cmnd[0] == INQUIRY && !islogical ) {
sgl = scsi_sglist(cmd);
if( sg_page(sgl) ) {
c = *(unsigned char *) sg_virt(&sgl[0]);
} else {
printk(KERN_WARNING
"megaraid: invalid sg.\n");
c = 0;
}
if(IS_RAID_CH(adapter, cmd->device->channel) &&
((c & 0x1F ) == TYPE_DISK)) {
status = 0xF0;
}
}
/* clear result; otherwise, success returns corrupt value */
cmd->result = 0;
/* Convert MegaRAID status to Linux error code */
switch (status) {
case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
cmd->result |= (DID_OK << 16);
break;
case 0x02: /* ERROR_ABORTED, i.e.
SCSI_STATUS_CHECK_CONDITION */
/* set sense_buffer and result fields */
if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
memcpy(cmd->sense_buffer, pthru->reqsensearea,
14);
cmd->result = (DRIVER_SENSE << 24) |
(DID_OK << 16) |
(CHECK_CONDITION << 1);
}
else {
if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
memcpy(cmd->sense_buffer,
epthru->reqsensearea, 14);
cmd->result = (DRIVER_SENSE << 24) |
(DID_OK << 16) |
(CHECK_CONDITION << 1);
} else {
cmd->sense_buffer[0] = 0x70;
cmd->sense_buffer[2] = ABORTED_COMMAND;
cmd->result |= (CHECK_CONDITION << 1);
}
}
break;
case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
SCSI_STATUS_BUSY */
cmd->result |= (DID_BUS_BUSY << 16) | status;
break;
default:
#if MEGA_HAVE_CLUSTERING
/*
* If TEST_UNIT_READY fails, we know
* MEGA_RESERVATION_STATUS failed
*/
if( cmd->cmnd[0] == TEST_UNIT_READY ) {
cmd->result |= (DID_ERROR << 16) |
(RESERVATION_CONFLICT << 1);
}
else
/*
* Error code returned is 1 if Reserve or Release
* failed or the input parameter is invalid
*/
if( status == 1 &&
(cmd->cmnd[0] == RESERVE ||
cmd->cmnd[0] == RELEASE) ) {
cmd->result |= (DID_ERROR << 16) |
(RESERVATION_CONFLICT << 1);
}
else
#endif
cmd->result |= (DID_BAD_TARGET << 16)|status;
}
/*
* Only free SCBs for the commands coming down from the
* mid-layer, not for which were issued internally
*
* For internal command, restore the status returned by the
* firmware so that user can interpret it.
*/
if( cmdid == CMDID_INT_CMDS ) { /* internal command */
cmd->result = status;
/*
* Remove the internal command from the pending list
*/
list_del_init(&scb->list);
scb->state = SCB_FREE;
}
else {
mega_free_scb(adapter, scb);
}
/* Add Scsi_Command to end of completed queue */
list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
}
}
/*
* mega_runpendq()
*
* Run through the list of completed requests and finish it
*/
static void
mega_rundoneq (adapter_t *adapter)
{
Scsi_Cmnd *cmd;
struct list_head *pos;
list_for_each(pos, &adapter->completed_list) {
struct scsi_pointer* spos = (struct scsi_pointer *)pos;
cmd = list_entry(spos, Scsi_Cmnd, SCp);
cmd->scsi_done(cmd);
}
INIT_LIST_HEAD(&adapter->completed_list);
}
/*
* Free a SCB structure
* Note: We assume the scsi commands associated with this scb is not free yet.
*/
static void
mega_free_scb(adapter_t *adapter, scb_t *scb)
{
switch( scb->dma_type ) {
case MEGA_DMA_TYPE_NONE:
break;
case MEGA_SGLIST:
scsi_dma_unmap(scb->cmd);
break;
default:
break;
}
/*
* Remove from the pending list
*/
list_del_init(&scb->list);
/* Link the scb back into free list */
scb->state = SCB_FREE;
scb->cmd = NULL;
list_add(&scb->list, &adapter->free_list);
}
static int
__mega_busywait_mbox (adapter_t *adapter)
{
volatile mbox_t *mbox = adapter->mbox;
long counter;
for (counter = 0; counter < 10000; counter++) {
if (!mbox->m_in.busy)
return 0;
udelay(100);
cond_resched();
}
return -1; /* give up after 1 second */
}
/*
* Copies data to SGLIST
* Note: For 64 bit cards, we need a minimum of one SG element for read/write
*/
static int
mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
{
struct scatterlist *sg;
Scsi_Cmnd *cmd;
int sgcnt;
int idx;
cmd = scb->cmd;
/*
* Copy Scatter-Gather list info into controller structure.
*
* The number of sg elements returned must not exceed our limit
*/
sgcnt = scsi_dma_map(cmd);
scb->dma_type = MEGA_SGLIST;
BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
*len = 0;
if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
sg = scsi_sglist(cmd);
scb->dma_h_bulkdata = sg_dma_address(sg);
*buf = (u32)scb->dma_h_bulkdata;
*len = sg_dma_len(sg);
return 0;
}
scsi_for_each_sg(cmd, sg, sgcnt, idx) {
if (adapter->has_64bit_addr) {
scb->sgl64[idx].address = sg_dma_address(sg);
*len += scb->sgl64[idx].length = sg_dma_len(sg);
} else {
scb->sgl[idx].address = sg_dma_address(sg);
*len += scb->sgl[idx].length = sg_dma_len(sg);
}
}
/* Reset pointer and length fields */
*buf = scb->sgl_dma_addr;
/* Return count of SG requests */
return sgcnt;
}
/*
* mega_8_to_40ld()
*
* takes all info in AdapterInquiry structure and puts it into ProductInfo and
* Enquiry3 structures for later use
*/
static void
mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
mega_product_info *product_info)
{
int i;
product_info->max_commands = inquiry->adapter_info.max_commands;
enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
product_info->nchannels = inquiry->adapter_info.nchannels;
for (i = 0; i < 4; i++) {
product_info->fw_version[i] =
inquiry->adapter_info.fw_version[i];
product_info->bios_version[i] =
inquiry->adapter_info.bios_version[i];
}
enquiry3->cache_flush_interval =
inquiry->adapter_info.cache_flush_interval;
product_info->dram_size = inquiry->adapter_info.dram_size;
enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
}
for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
}
static inline void
mega_free_sgl(adapter_t *adapter)
{
scb_t *scb;
int i;
for(i = 0; i < adapter->max_cmds; i++) {
scb = &adapter->scb_list[i];
if( scb->sgl64 ) {
pci_free_consistent(adapter->dev,
sizeof(mega_sgl64) * adapter->sglen,
scb->sgl64,
scb->sgl_dma_addr);
scb->sgl64 = NULL;
}
if( scb->pthru ) {
pci_free_consistent(adapter->dev, sizeof(mega_passthru),
scb->pthru, scb->pthru_dma_addr);
scb->pthru = NULL;
}
if( scb->epthru ) {
pci_free_consistent(adapter->dev,
sizeof(mega_ext_passthru),
scb->epthru, scb->epthru_dma_addr);
scb->epthru = NULL;
}
}
}
/*
* Get information about the card/driver
*/
const char *
megaraid_info(struct Scsi_Host *host)
{
static char buffer[512];
adapter_t *adapter;
adapter = (adapter_t *)host->hostdata;
sprintf (buffer,
"LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
adapter->fw_version, adapter->product_info.max_commands,
adapter->host->max_id, adapter->host->max_channel,
adapter->host->max_lun);
return buffer;
}
/*
* Abort a previous SCSI request. Only commands on the pending list can be
* aborted. All the commands issued to the F/W must complete.
*/
static int
megaraid_abort(Scsi_Cmnd *cmd)
{
adapter_t *adapter;
int rval;
adapter = (adapter_t *)cmd->device->host->hostdata;
rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
/*
* This is required here to complete any completed requests
* to be communicated over to the mid layer.
*/
mega_rundoneq(adapter);
return rval;
}
static int
megaraid_reset(struct scsi_cmnd *cmd)
{
adapter_t *adapter;
megacmd_t mc;
int rval;
adapter = (adapter_t *)cmd->device->host->hostdata;
#if MEGA_HAVE_CLUSTERING
mc.cmd = MEGA_CLUSTER_CMD;
mc.opcode = MEGA_RESET_RESERVATIONS;
if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
printk(KERN_WARNING
"megaraid: reservation reset failed.\n");
}
else {
printk(KERN_INFO "megaraid: reservation reset.\n");
}
#endif
spin_lock_irq(&adapter->lock);
rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
/*
* This is required here to complete any completed requests
* to be communicated over to the mid layer.
*/
mega_rundoneq(adapter);
spin_unlock_irq(&adapter->lock);
return rval;
}
/**
* megaraid_abort_and_reset()
* @adapter - megaraid soft state
* @cmd - scsi command to be aborted or reset
* @aor - abort or reset flag
*
* Try to locate the scsi command in the pending queue. If found and is not
* issued to the controller, abort/reset it. Otherwise return failure
*/
static int
megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
{
struct list_head *pos, *next;
scb_t *scb;
printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
(aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
cmd->cmnd[0], cmd->device->channel,
cmd->device->id, cmd->device->lun);
if(list_empty(&adapter->pending_list))
return FALSE;
list_for_each_safe(pos, next, &adapter->pending_list) {
scb = list_entry(pos, scb_t, list);
if (scb->cmd == cmd) { /* Found command */
scb->state |= aor;
/*
* Check if this command has firmware ownership. If
* yes, we cannot reset this command. Whenever f/w
* completes this command, we will return appropriate
* status from ISR.
*/
if( scb->state & SCB_ISSUED ) {
printk(KERN_WARNING
"megaraid: %s-%lx[%x], fw owner.\n",
(aor==SCB_ABORT) ? "ABORTING":"RESET",
cmd->serial_number, scb->idx);
return FALSE;
}
else {
/*
* Not yet issued! Remove from the pending
* list
*/
printk(KERN_WARNING
"megaraid: %s-%lx[%x], driver owner.\n",
(aor==SCB_ABORT) ? "ABORTING":"RESET",
cmd->serial_number, scb->idx);
mega_free_scb(adapter, scb);
if( aor == SCB_ABORT ) {
cmd->result = (DID_ABORT << 16);
}
else {
cmd->result = (DID_RESET << 16);
}
list_add_tail(SCSI_LIST(cmd),
&adapter->completed_list);
return TRUE;
}
}
}
return FALSE;
}
static inline int
make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
{
*pdev = alloc_pci_dev();
if( *pdev == NULL ) return -1;
memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
kfree(*pdev);
return -1;
}
return 0;
}
static inline void
free_local_pdev(struct pci_dev *pdev)
{
kfree(pdev);
}
/**
* mega_allocate_inquiry()
* @dma_handle - handle returned for dma address
* @pdev - handle to pci device
*
* allocates memory for inquiry structure
*/
static inline void *
mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
{
return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
}
static inline void
mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
{
pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
}
#ifdef CONFIG_PROC_FS
/* Following code handles /proc fs */
#define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
S_IRUSR | S_IFREG, \
controller_proc_dir_entry, \
func, adapter)
/**
* mega_create_proc_entry()
* @index - index in soft state array
* @parent - parent node for this /proc entry
*
* Creates /proc entries for our controllers.
*/
static void
mega_create_proc_entry(int index, struct proc_dir_entry *parent)
{
struct proc_dir_entry *controller_proc_dir_entry = NULL;
u8 string[64] = { 0 };
adapter_t *adapter = hba_soft_state[index];
sprintf(string, "hba%d", adapter->host->host_no);
controller_proc_dir_entry =
adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
if(!controller_proc_dir_entry) {
printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
return;
}
adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
#if MEGA_HAVE_ENH_PROC
adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
adapter->proc_battery = CREATE_READ_PROC("battery-status",
proc_battery);
/*
* Display each physical drive on its channel
*/
adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
proc_pdrv_ch0);
adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
proc_pdrv_ch1);
adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
proc_pdrv_ch2);
adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
proc_pdrv_ch3);
/*
* Display a set of up to 10 logical drive through each of following
* /proc entries
*/
adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
proc_rdrv_10);
adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
proc_rdrv_20);
adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
proc_rdrv_30);
adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
proc_rdrv_40);
#endif
}
/**
* proc_read_config()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display configuration information about the controller.
*/
static int
proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
int len = 0;
len += sprintf(page+len, "%s", MEGARAID_VERSION);
if(adapter->product_info.product_name[0])
len += sprintf(page+len, "%s\n",
adapter->product_info.product_name);
len += sprintf(page+len, "Controller Type: ");
if( adapter->flag & BOARD_MEMMAP ) {
len += sprintf(page+len,
"438/466/467/471/493/518/520/531/532\n");
}
else {
len += sprintf(page+len,
"418/428/434\n");
}
if(adapter->flag & BOARD_40LD) {
len += sprintf(page+len,
"Controller Supports 40 Logical Drives\n");
}
if(adapter->flag & BOARD_64BIT) {
len += sprintf(page+len,
"Controller capable of 64-bit memory addressing\n");
}
if( adapter->has_64bit_addr ) {
len += sprintf(page+len,
"Controller using 64-bit memory addressing\n");
}
else {
len += sprintf(page+len,
"Controller is not using 64-bit memory addressing\n");
}
len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
adapter->host->irq);
len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
adapter->numldrv, adapter->product_info.nchannels);
len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
adapter->fw_version, adapter->bios_version,
adapter->product_info.dram_size);
len += sprintf(page+len,
"Controller Queue Depth = %d, Driver Queue Depth = %d\n",
adapter->product_info.max_commands, adapter->max_cmds);
len += sprintf(page+len, "support_ext_cdb = %d\n",
adapter->support_ext_cdb);
len += sprintf(page+len, "support_random_del = %d\n",
adapter->support_random_del);
len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
adapter->boot_ldrv_enabled);
len += sprintf(page+len, "boot_ldrv = %d\n",
adapter->boot_ldrv);
len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
adapter->boot_pdrv_enabled);
len += sprintf(page+len, "boot_pdrv_ch = %d\n",
adapter->boot_pdrv_ch);
len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
adapter->boot_pdrv_tgt);
len += sprintf(page+len, "quiescent = %d\n",
atomic_read(&adapter->quiescent));
len += sprintf(page+len, "has_cluster = %d\n",
adapter->has_cluster);
len += sprintf(page+len, "\nModule Parameters:\n");
len += sprintf(page+len, "max_cmd_per_lun = %d\n",
max_cmd_per_lun);
len += sprintf(page+len, "max_sectors_per_io = %d\n",
max_sectors_per_io);
*eof = 1;
return len;
}
/**
* proc_read_stat()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Diaplay statistical information about the I/O activity.
*/
static int
proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter;
int len;
int i;
i = 0; /* avoid compilation warnings */
len = 0;
adapter = (adapter_t *)data;
len = sprintf(page, "Statistical Information for this controller\n");
len += sprintf(page+len, "pend_cmds = %d\n",
atomic_read(&adapter->pend_cmds));
#if MEGA_HAVE_STATS
for(i = 0; i < adapter->numldrv; i++) {
len += sprintf(page+len, "Logical Drive %d:\n", i);
len += sprintf(page+len,
"\tReads Issued = %lu, Writes Issued = %lu\n",
adapter->nreads[i], adapter->nwrites[i]);
len += sprintf(page+len,
"\tSectors Read = %lu, Sectors Written = %lu\n",
adapter->nreadblocks[i], adapter->nwriteblocks[i]);
len += sprintf(page+len,
"\tRead errors = %lu, Write errors = %lu\n\n",
adapter->rd_errors[i], adapter->wr_errors[i]);
}
#else
len += sprintf(page+len,
"IO and error counters not compiled in driver.\n");
#endif
*eof = 1;
return len;
}
/**
* proc_read_mbox()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display mailbox information for the last command issued. This information
* is good for debugging.
*/
static int
proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
volatile mbox_t *mbox = adapter->mbox;
int len = 0;
len = sprintf(page, "Contents of Mail Box Structure\n");
len += sprintf(page+len, " Fw Command = 0x%02x\n",
mbox->m_out.cmd);
len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
mbox->m_out.cmdid);
len += sprintf(page+len, " No of Sectors= %04d\n",
mbox->m_out.numsectors);
len += sprintf(page+len, " LBA = 0x%02x\n",
mbox->m_out.lba);
len += sprintf(page+len, " DTA = 0x%08x\n",
mbox->m_out.xferaddr);
len += sprintf(page+len, " Logical Drive= 0x%02x\n",
mbox->m_out.logdrv);
len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
mbox->m_out.numsgelements);
len += sprintf(page+len, " Busy = %01x\n",
mbox->m_in.busy);
len += sprintf(page+len, " Status = 0x%02x\n",
mbox->m_in.status);
*eof = 1;
return len;
}
/**
* proc_rebuild_rate()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display current rebuild rate
*/
static int
proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
dma_addr_t dma_handle;
caddr_t inquiry;
struct pci_dev *pdev;
int len = 0;
if( make_local_pdev(adapter, &pdev) != 0 ) {
*eof = 1;
return len;
}
if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
free_local_pdev(pdev);
*eof = 1;
return len;
}
if( mega_adapinq(adapter, dma_handle) != 0 ) {
len = sprintf(page, "Adapter inquiry failed.\n");
printk(KERN_WARNING "megaraid: inquiry failed.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
*eof = 1;
return len;
}
if( adapter->flag & BOARD_40LD ) {
len = sprintf(page, "Rebuild Rate: [%d%%]\n",
((mega_inquiry3 *)inquiry)->rebuild_rate);
}
else {
len = sprintf(page, "Rebuild Rate: [%d%%]\n",
((mraid_ext_inquiry *)
inquiry)->raid_inq.adapter_info.rebuild_rate);
}
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
*eof = 1;
return len;
}
/**
* proc_battery()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display information about the battery module on the controller.
*/
static int
proc_battery(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
dma_addr_t dma_handle;
caddr_t inquiry;
struct pci_dev *pdev;
u8 battery_status = 0;
char str[256];
int len = 0;
if( make_local_pdev(adapter, &pdev) != 0 ) {
*eof = 1;
return len;
}
if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
free_local_pdev(pdev);
*eof = 1;
return len;
}
if( mega_adapinq(adapter, dma_handle) != 0 ) {
len = sprintf(page, "Adapter inquiry failed.\n");
printk(KERN_WARNING "megaraid: inquiry failed.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
*eof = 1;
return len;
}
if( adapter->flag & BOARD_40LD ) {
battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
}
else {
battery_status = ((mraid_ext_inquiry *)inquiry)->
raid_inq.adapter_info.battery_status;
}
/*
* Decode the battery status
*/
sprintf(str, "Battery Status:[%d]", battery_status);
if(battery_status == MEGA_BATT_CHARGE_DONE)
strcat(str, " Charge Done");
if(battery_status & MEGA_BATT_MODULE_MISSING)
strcat(str, " Module Missing");
if(battery_status & MEGA_BATT_LOW_VOLTAGE)
strcat(str, " Low Voltage");
if(battery_status & MEGA_BATT_TEMP_HIGH)
strcat(str, " Temperature High");
if(battery_status & MEGA_BATT_PACK_MISSING)
strcat(str, " Pack Missing");
if(battery_status & MEGA_BATT_CHARGE_INPROG)
strcat(str, " Charge In-progress");
if(battery_status & MEGA_BATT_CHARGE_FAIL)
strcat(str, " Charge Fail");
if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
strcat(str, " Cycles Exceeded");
len = sprintf(page, "%s\n", str);
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
*eof = 1;
return len;
}
/**
* proc_pdrv_ch0()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display information about the physical drives on physical channel 0.
*/
static int
proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_pdrv(adapter, page, 0));
}
/**
* proc_pdrv_ch1()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display information about the physical drives on physical channel 1.
*/
static int
proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_pdrv(adapter, page, 1));
}
/**
* proc_pdrv_ch2()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display information about the physical drives on physical channel 2.
*/
static int
proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_pdrv(adapter, page, 2));
}
/**
* proc_pdrv_ch3()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display information about the physical drives on physical channel 3.
*/
static int
proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_pdrv(adapter, page, 3));
}
/**
* proc_pdrv()
* @page - buffer to write the data in
* @adapter - pointer to our soft state
*
* Display information about the physical drives.
*/
static int
proc_pdrv(adapter_t *adapter, char *page, int channel)
{
dma_addr_t dma_handle;
char *scsi_inq;
dma_addr_t scsi_inq_dma_handle;
caddr_t inquiry;
struct pci_dev *pdev;
u8 *pdrv_state;
u8 state;
int tgt;
int max_channels;
int len = 0;
char str[80];
int i;
if( make_local_pdev(adapter, &pdev) != 0 ) {
return len;
}
if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
goto free_pdev;
}
if( mega_adapinq(adapter, dma_handle) != 0 ) {
len = sprintf(page, "Adapter inquiry failed.\n");
printk(KERN_WARNING "megaraid: inquiry failed.\n");
goto free_inquiry;
}
scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
if( scsi_inq == NULL ) {
len = sprintf(page, "memory not available for scsi inq.\n");
goto free_inquiry;
}
if( adapter->flag & BOARD_40LD ) {
pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
}
else {
pdrv_state = ((mraid_ext_inquiry *)inquiry)->
raid_inq.pdrv_info.pdrv_state;
}
max_channels = adapter->product_info.nchannels;
if( channel >= max_channels ) {
goto free_pci;
}
for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
i = channel*16 + tgt;
state = *(pdrv_state + i);
switch( state & 0x0F ) {
case PDRV_ONLINE:
sprintf(str,
"Channel:%2d Id:%2d State: Online",
channel, tgt);
break;
case PDRV_FAILED:
sprintf(str,
"Channel:%2d Id:%2d State: Failed",
channel, tgt);
break;
case PDRV_RBLD:
sprintf(str,
"Channel:%2d Id:%2d State: Rebuild",
channel, tgt);
break;
case PDRV_HOTSPARE:
sprintf(str,
"Channel:%2d Id:%2d State: Hot spare",
channel, tgt);
break;
default:
sprintf(str,
"Channel:%2d Id:%2d State: Un-configured",
channel, tgt);
break;
}
/*
* This interface displays inquiries for disk drives
* only. Inquries for logical drives and non-disk
* devices are available through /proc/scsi/scsi
*/
memset(scsi_inq, 0, 256);
if( mega_internal_dev_inquiry(adapter, channel, tgt,
scsi_inq_dma_handle) ||
(scsi_inq[0] & 0x1F) != TYPE_DISK ) {
continue;
}
/*
* Check for overflow. We print less than 240
* characters for inquiry
*/
if( (len + 240) >= PAGE_SIZE ) break;
len += sprintf(page+len, "%s.\n", str);
len += mega_print_inquiry(page+len, scsi_inq);
}
free_pci:
pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
free_inquiry:
mega_free_inquiry(inquiry, dma_handle, pdev);
free_pdev:
free_local_pdev(pdev);
return len;
}
/*
* Display scsi inquiry
*/
static int
mega_print_inquiry(char *page, char *scsi_inq)
{
int len = 0;
int i;
len = sprintf(page, " Vendor: ");
for( i = 8; i < 16; i++ ) {
len += sprintf(page+len, "%c", scsi_inq[i]);
}
len += sprintf(page+len, " Model: ");
for( i = 16; i < 32; i++ ) {
len += sprintf(page+len, "%c", scsi_inq[i]);
}
len += sprintf(page+len, " Rev: ");
for( i = 32; i < 36; i++ ) {
len += sprintf(page+len, "%c", scsi_inq[i]);
}
len += sprintf(page+len, "\n");
i = scsi_inq[0] & 0x1f;
len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
len += sprintf(page+len,
" ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
len += sprintf(page+len, " CCS\n");
else
len += sprintf(page+len, "\n");
return len;
}
/**
* proc_rdrv_10()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display real time information about the logical drives 0 through 9.
*/
static int
proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_rdrv(adapter, page, 0, 9));
}
/**
* proc_rdrv_20()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display real time information about the logical drives 0 through 9.
*/
static int
proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_rdrv(adapter, page, 10, 19));
}
/**
* proc_rdrv_30()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display real time information about the logical drives 0 through 9.
*/
static int
proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_rdrv(adapter, page, 20, 29));
}
/**
* proc_rdrv_40()
* @page - buffer to write the data in
* @start - where the actual data has been written in page
* @offset - same meaning as the read system call
* @count - same meaning as the read system call
* @eof - set if no more data needs to be returned
* @data - pointer to our soft state
*
* Display real time information about the logical drives 0 through 9.
*/
static int
proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
void *data)
{
adapter_t *adapter = (adapter_t *)data;
*eof = 1;
return (proc_rdrv(adapter, page, 30, 39));
}
/**
* proc_rdrv()
* @page - buffer to write the data in
* @adapter - pointer to our soft state
* @start - starting logical drive to display
* @end - ending logical drive to display
*
* We do not print the inquiry information since its already available through
* /proc/scsi/scsi interface
*/
static int
proc_rdrv(adapter_t *adapter, char *page, int start, int end )
{
dma_addr_t dma_handle;
logdrv_param *lparam;
megacmd_t mc;
char *disk_array;
dma_addr_t disk_array_dma_handle;
caddr_t inquiry;
struct pci_dev *pdev;
u8 *rdrv_state;
int num_ldrv;
u32 array_sz;
int len = 0;
int i;
if( make_local_pdev(adapter, &pdev) != 0 ) {
return len;
}
if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
free_local_pdev(pdev);
return len;
}
if( mega_adapinq(adapter, dma_handle) != 0 ) {
len = sprintf(page, "Adapter inquiry failed.\n");
printk(KERN_WARNING "megaraid: inquiry failed.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
return len;
}
memset(&mc, 0, sizeof(megacmd_t));
if( adapter->flag & BOARD_40LD ) {
array_sz = sizeof(disk_array_40ld);
rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
}
else {
array_sz = sizeof(disk_array_8ld);
rdrv_state = ((mraid_ext_inquiry *)inquiry)->
raid_inq.logdrv_info.ldrv_state;
num_ldrv = ((mraid_ext_inquiry *)inquiry)->
raid_inq.logdrv_info.num_ldrv;
}
disk_array = pci_alloc_consistent(pdev, array_sz,
&disk_array_dma_handle);
if( disk_array == NULL ) {
len = sprintf(page, "memory not available.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
free_local_pdev(pdev);
return len;
}
mc.xferaddr = (u32)disk_array_dma_handle;
if( adapter->flag & BOARD_40LD ) {
mc.cmd = FC_NEW_CONFIG;
mc.opcode = OP_DCMD_READ_CONFIG;
if( mega_internal_command(adapter, &mc, NULL) ) {
len = sprintf(page, "40LD read config failed.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
pci_free_consistent(pdev, array_sz, disk_array,
disk_array_dma_handle);
free_local_pdev(pdev);
return len;
}
}
else {
mc.cmd = NEW_READ_CONFIG_8LD;
if( mega_internal_command(adapter, &mc, NULL) ) {
mc.cmd = READ_CONFIG_8LD;
if( mega_internal_command(adapter, &mc,
NULL) ){
len = sprintf(page,
"8LD read config failed.\n");
mega_free_inquiry(inquiry, dma_handle, pdev);
pci_free_consistent(pdev, array_sz,
disk_array,
disk_array_dma_handle);
free_local_pdev(pdev);
return len;
}
}
}
for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
if( adapter->flag & BOARD_40LD ) {
lparam =
&((disk_array_40ld *)disk_array)->ldrv[i].lparam;
}
else {
lparam =
&((disk_array_8ld *)disk_array)->ldrv[i].lparam;
}
/*
* Check for overflow. We print less than 240 characters for
* information about each logical drive.
*/
if( (len + 240) >= PAGE_SIZE ) break;
len += sprintf(page+len, "Logical drive:%2d:, ", i);
switch( rdrv_state[i] & 0x0F ) {
case RDRV_OFFLINE:
len += sprintf(page+len, "state: offline");
break;
case RDRV_DEGRADED:
len += sprintf(page+len, "state: degraded");
break;
case RDRV_OPTIMAL:
len += sprintf(page+len, "state: optimal");
break;
case RDRV_DELETED:
len += sprintf(page+len, "state: deleted");
break;
default:
len += sprintf(page+len, "state: unknown");
break;
}
/*
* Check if check consistency or initialization is going on
* for this logical drive.
*/
if( (rdrv_state[i] & 0xF0) == 0x20 ) {
len += sprintf(page+len,
", check-consistency in progress");
}
else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
len += sprintf(page+len,
", initialization in progress");
}
len += sprintf(page+len, "\n");
len += sprintf(page+len, "Span depth:%3d, ",
lparam->span_depth);
len += sprintf(page+len, "RAID level:%3d, ",
lparam->level);
len += sprintf(page+len, "Stripe size:%3d, ",
lparam->stripe_sz ? lparam->stripe_sz/2: 128);
len += sprintf(page+len, "Row size:%3d\n",
lparam->row_size);
len += sprintf(page+len, "Read Policy: ");
switch(lparam->read_ahead) {
case NO_READ_AHEAD:
len += sprintf(page+len, "No read ahead, ");
break;
case READ_AHEAD:
len += sprintf(page+len, "Read ahead, ");
break;
case ADAP_READ_AHEAD:
len += sprintf(page+len, "Adaptive, ");
break;
}
len += sprintf(page+len, "Write Policy: ");
switch(lparam->write_mode) {
case WRMODE_WRITE_THRU:
len += sprintf(page+len, "Write thru, ");
break;
case WRMODE_WRITE_BACK:
len += sprintf(page+len, "Write back, ");
break;
}
len += sprintf(page+len, "Cache Policy: ");
switch(lparam->direct_io) {
case CACHED_IO:
len += sprintf(page+len, "Cached IO\n\n");
break;
case DIRECT_IO:
len += sprintf(page+len, "Direct IO\n\n");
break;
}
}
mega_free_inquiry(inquiry, dma_handle, pdev);
pci_free_consistent(pdev, array_sz, disk_array,
disk_array_dma_handle);
free_local_pdev(pdev);
return len;
}
#else
static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
{
}
#endif
/**
* megaraid_biosparam()
*
* Return the disk geometry for a particular disk
*/
static int
megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
sector_t capacity, int geom[])
{
adapter_t *adapter;
unsigned char *bh;
int heads;
int sectors;
int cylinders;
int rval;
/* Get pointer to host config structure */
adapter = (adapter_t *)sdev->host->hostdata;
if (IS_RAID_CH(adapter, sdev->channel)) {
/* Default heads (64) & sectors (32) */
heads = 64;
sectors = 32;
cylinders = (ulong)capacity / (heads * sectors);
/*
* Handle extended translation size for logical drives
* > 1Gb
*/
if ((ulong)capacity >= 0x200000) {
heads = 255;
sectors = 63;
cylinders = (ulong)capacity / (heads * sectors);
}
/* return result */
geom[0] = heads;
geom[1] = sectors;
geom[2] = cylinders;
}
else {
bh = scsi_bios_ptable(bdev);
if( bh ) {
rval = scsi_partsize(bh, capacity,
&geom[2], &geom[0], &geom[1]);
kfree(bh);
if( rval != -1 )
return rval;
}
printk(KERN_INFO
"megaraid: invalid partition on this disk on channel %d\n",
sdev->channel);
/* Default heads (64) & sectors (32) */
heads = 64;
sectors = 32;
cylinders = (ulong)capacity / (heads * sectors);
/* Handle extended translation size for logical drives > 1Gb */
if ((ulong)capacity >= 0x200000) {
heads = 255;
sectors = 63;
cylinders = (ulong)capacity / (heads * sectors);
}
/* return result */
geom[0] = heads;
geom[1] = sectors;
geom[2] = cylinders;
}
return 0;
}
/**
* mega_init_scb()
* @adapter - pointer to our soft state
*
* Allocate memory for the various pointers in the scb structures:
* scatter-gather list pointer, passthru and extended passthru structure
* pointers.
*/
static int
mega_init_scb(adapter_t *adapter)
{
scb_t *scb;
int i;
for( i = 0; i < adapter->max_cmds; i++ ) {
scb = &adapter->scb_list[i];
scb->sgl64 = NULL;
scb->sgl = NULL;
scb->pthru = NULL;
scb->epthru = NULL;
}
for( i = 0; i < adapter->max_cmds; i++ ) {
scb = &adapter->scb_list[i];
scb->idx = i;
scb->sgl64 = pci_alloc_consistent(adapter->dev,
sizeof(mega_sgl64) * adapter->sglen,
&scb->sgl_dma_addr);
scb->sgl = (mega_sglist *)scb->sgl64;
if( !scb->sgl ) {
printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
mega_free_sgl(adapter);
return -1;
}
scb->pthru = pci_alloc_consistent(adapter->dev,
sizeof(mega_passthru),
&scb->pthru_dma_addr);
if( !scb->pthru ) {
printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
mega_free_sgl(adapter);
return -1;
}
scb->epthru = pci_alloc_consistent(adapter->dev,
sizeof(mega_ext_passthru),
&scb->epthru_dma_addr);
if( !scb->epthru ) {