cciss.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,278 行 · 第 1/5 页
C
2,278 行
/* * Disk Array driver for HP SA 5xxx and 6xxx Controllers * Copyright 2000, 2002 Hewlett-Packard Development Company, L.P. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or * NON INFRINGEMENT. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Questions/Comments/Bugfixes to Cciss-discuss@lists.sourceforge.net * */#include <linux/config.h> /* CONFIG_PROC_FS */#include <linux/module.h>#include <linux/interrupt.h>#include <linux/types.h>#include <linux/pci.h>#include <linux/kernel.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/major.h>#include <linux/fs.h>#include <linux/bio.h>#include <linux/blkpg.h>#include <linux/timer.h>#include <linux/proc_fs.h>#include <linux/init.h> #include <linux/hdreg.h>#include <linux/spinlock.h>#include <linux/compat.h>#include <asm/uaccess.h>#include <asm/io.h>#include <linux/blkdev.h>#include <linux/genhd.h>#include <linux/completion.h>#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))#define DRIVER_NAME "HP CISS Driver (v 2.6.2)"#define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,2)/* Embedded module documentation macros - see modules.h */MODULE_AUTHOR("Hewlett-Packard Company");MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.2");MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400" " SA6i V100");MODULE_LICENSE("GPL");#include "cciss_cmd.h"#include "cciss.h"#include <linux/cciss_ioctl.h>/* define the PCI info for the cards we can control */const struct pci_device_id cciss_pci_device_id[] = { { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091, 0, 0, 0}, { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409E, 0, 0, 0}, { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISS, 0x103C, 0x3211, 0, 0, 0}, {0,}};MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);#define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))/* board_id = Subsystem Device ID & Vendor ID * product = Marketing Name for the board * access = Address of the struct of function pointers */static struct board_type products[] = { { 0x40700E11, "Smart Array 5300", &SA5_access }, { 0x40800E11, "Smart Array 5i", &SA5B_access}, { 0x40820E11, "Smart Array 532", &SA5B_access}, { 0x40830E11, "Smart Array 5312", &SA5B_access}, { 0x409A0E11, "Smart Array 641", &SA5_access}, { 0x409B0E11, "Smart Array 642", &SA5_access}, { 0x409C0E11, "Smart Array 6400", &SA5_access}, { 0x409D0E11, "Smart Array 6400 EM", &SA5_access}, { 0x40910E11, "Smart Array 6i", &SA5_access}, { 0x409E0E11, "Smart Array 6422", &SA5_access}, { 0x3211103C, "Smart Array V100", &SA5_access},};/* How long to wait (in millesconds) for board to go into simple mode */#define MAX_CONFIG_WAIT 30000 #define MAX_IOCTL_CONFIG_WAIT 1000/*define how many times we will try a command because of bus resets */#define MAX_CMD_RETRIES 3#define READ_AHEAD 1024#define NR_CMDS 384 /* #commands that can be outstanding */#define MAX_CTLR 8#define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */static ctlr_info_t *hba[MAX_CTLR];static void do_cciss_request(request_queue_t *q);static int cciss_open(struct inode *inode, struct file *filep);static int cciss_release(struct inode *inode, struct file *filep);static int cciss_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);static int revalidate_allvol(ctlr_info_t *host);static int cciss_revalidate(struct gendisk *disk);static int deregister_disk(struct gendisk *disk);static int register_new_disk(ctlr_info_t *h);static void cciss_getgeometry(int cntl_num);static void start_io( ctlr_info_t *h);static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr, int cmd_type);#ifdef CONFIG_PROC_FSstatic int cciss_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data);static void cciss_procinit(int i);#elsestatic void cciss_procinit(int i) {}#endif /* CONFIG_PROC_FS */static struct block_device_operations cciss_fops = { .owner = THIS_MODULE, .open = cciss_open, .release = cciss_release, .ioctl = cciss_ioctl, .revalidate_disk= cciss_revalidate,};/* * Enqueuing and dequeuing functions for cmdlists. */static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c){ if (*Qptr == NULL) { *Qptr = c; c->next = c->prev = c; } else { c->prev = (*Qptr)->prev; c->next = (*Qptr); (*Qptr)->prev->next = c; (*Qptr)->prev = c; }}static inline CommandList_struct *removeQ(CommandList_struct **Qptr, CommandList_struct *c){ if (c && c->next != c) { if (*Qptr == c) *Qptr = c->next; c->prev->next = c->next; c->next->prev = c->prev; } else { *Qptr = NULL; } return c;}#ifdef CONFIG_PROC_FS#include "cciss_scsi.c" /* For SCSI tape support *//* * Report information about this controller. */#define ENG_GIG 1000000000#define ENG_GIG_FACTOR (ENG_GIG/512)#define RAID_UNKNOWN 6static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG", "UNKNOWN"};static struct proc_dir_entry *proc_cciss;static int cciss_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data){ off_t pos = 0; off_t len = 0; int size, i, ctlr; ctlr_info_t *h = (ctlr_info_t*)data; drive_info_struct *drv; unsigned long flags; sector_t vol_sz, vol_sz_frac; ctlr = h->ctlr; /* prevent displaying bogus info during configuration * or deconfiguration of a logical volume */ spin_lock_irqsave(CCISS_LOCK(ctlr), flags); if (h->busy_configuring) { spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); return -EBUSY; } h->busy_configuring = 1; spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags); size = sprintf(buffer, "%s: HP %s Controller\n" "Board ID: 0x%08lx\n" "Firmware Version: %c%c%c%c\n" "IRQ: %d\n" "Logical drives: %d\n" "Current Q depth: %d\n" "Current # commands on controller: %d\n" "Max Q depth since init: %d\n" "Max # commands on controller since init: %d\n" "Max SG entries since init: %d\n\n", h->devname, h->product_name, (unsigned long)h->board_id, h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3], (unsigned int)h->intr, h->num_luns, h->Qdepth, h->commands_outstanding, h->maxQsinceinit, h->max_outstanding, h->maxSG); pos += size; len += size; cciss_proc_tape_report(ctlr, buffer, &pos, &len); for(i=0; i<=h->highest_lun; i++) { drv = &h->drv[i]; if (drv->block_size == 0) continue; vol_sz = drv->nr_blocks; vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR); vol_sz_frac *= 100; sector_div(vol_sz_frac, ENG_GIG_FACTOR); if (drv->raid_level > 5) drv->raid_level = RAID_UNKNOWN; size = sprintf(buffer+len, "cciss/c%dd%d:" "\t%4u.%02uGB\tRAID %s\n", ctlr, i, (int)vol_sz, (int)vol_sz_frac, raid_label[drv->raid_level]); pos += size; len += size; } *eof = 1; *start = buffer+offset; len -= offset; if (len>length) len = length; h->busy_configuring = 0; return len;}static int cciss_proc_write(struct file *file, const char __user *buffer, unsigned long count, void *data){ unsigned char cmd[80]; int len;#ifdef CONFIG_CISS_SCSI_TAPE ctlr_info_t *h = (ctlr_info_t *) data; int rc;#endif if (count > sizeof(cmd)-1) return -EINVAL; if (copy_from_user(cmd, buffer, count)) return -EFAULT; cmd[count] = '\0'; len = strlen(cmd); // above 3 lines ensure safety if (cmd[len-1] == '\n') cmd[--len] = '\0';# ifdef CONFIG_CISS_SCSI_TAPE if (strcmp("engage scsi", cmd)==0) { rc = cciss_engage_scsi(h->ctlr); if (rc != 0) return -rc; return count; } /* might be nice to have "disengage" too, but it's not safely possible. (only 1 module use count, lock issues.) */# endif return -EINVAL;}/* * Get us a file in /proc/cciss that says something about each controller. * Create /proc/cciss if it doesn't exist yet. */static void __devinit cciss_procinit(int i){ struct proc_dir_entry *pde; if (proc_cciss == NULL) { proc_cciss = proc_mkdir("cciss", proc_root_driver); if (!proc_cciss) return; } pde = create_proc_read_entry(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, proc_cciss, cciss_proc_get_info, hba[i]); pde->write_proc = cciss_proc_write;}#endif /* CONFIG_PROC_FS *//* * For operations that cannot sleep, a command block is allocated at init, * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track * which ones are free or in use. For operations that can wait for kmalloc * to possible sleep, this routine can be called with get_from_pool set to 0. * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was. */ static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool){ CommandList_struct *c; int i; u64bit temp64; dma_addr_t cmd_dma_handle, err_dma_handle; if (!get_from_pool) { c = (CommandList_struct *) pci_alloc_consistent( h->pdev, sizeof(CommandList_struct), &cmd_dma_handle); if(c==NULL) return NULL; memset(c, 0, sizeof(CommandList_struct)); c->err_info = (ErrorInfo_struct *)pci_alloc_consistent( h->pdev, sizeof(ErrorInfo_struct), &err_dma_handle); if (c->err_info == NULL) { pci_free_consistent(h->pdev, sizeof(CommandList_struct), c, cmd_dma_handle); return NULL; } memset(c->err_info, 0, sizeof(ErrorInfo_struct)); } else /* get it out of the controllers pool */ { do { i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS); if (i == NR_CMDS) return NULL; } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);#ifdef CCISS_DEBUG printk(KERN_DEBUG "cciss: using command buffer %d\n", i);#endif c = h->cmd_pool + i; memset(c, 0, sizeof(CommandList_struct)); cmd_dma_handle = h->cmd_pool_dhandle + i*sizeof(CommandList_struct); c->err_info = h->errinfo_pool + i; memset(c->err_info, 0, sizeof(ErrorInfo_struct)); err_dma_handle = h->errinfo_pool_dhandle + i*sizeof(ErrorInfo_struct); h->nr_allocs++; } c->busaddr = (__u32) cmd_dma_handle; temp64.val = (__u64) err_dma_handle; c->ErrDesc.Addr.lower = temp64.val32.lower; c->ErrDesc.Addr.upper = temp64.val32.upper; c->ErrDesc.Len = sizeof(ErrorInfo_struct); c->ctlr = h->ctlr; return c;}/* * Frees a command block that was previously allocated with cmd_alloc(). */static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool){ int i; u64bit temp64; if( !got_from_pool) { temp64.val32.lower = c->ErrDesc.Addr.lower; temp64.val32.upper = c->ErrDesc.Addr.upper; pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct), c->err_info, (dma_addr_t) temp64.val); pci_free_consistent(h->pdev, sizeof(CommandList_struct), c, (dma_addr_t) c->busaddr); } else { i = c - h->cmd_pool; clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG)); h->nr_frees++; }}static inline ctlr_info_t *get_host(struct gendisk *disk){ return disk->queue->queuedata; }static inline drive_info_struct *get_drv(struct gendisk *disk){ return disk->private_data;}/* * Open. Make sure the device is really there. */static int cciss_open(struct inode *inode, struct file *filep){ ctlr_info_t *host = get_host(inode->i_bdev->bd_disk); drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);#ifdef CCISS_DEBUG printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);#endif /* CCISS_DEBUG */ /* * Root is allowed to open raw volume zero even if it's not configured * so array config can still work. I don't think I really like this, * but I'm already using way to many device nodes to claim another one * for "raw controller". */ if (drv->nr_blocks == 0) { if (iminor(inode) != 0) return -ENXIO; if (!capable(CAP_SYS_ADMIN)) return -EPERM; } drv->usage_count++; host->usage_count++; return 0;}/* * Close. Sync first.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?