⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cciss.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *    Disk Array driver for Compaq SMART2 Controllers *    Copyright 2000 Compaq Computer 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. * *    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 arrays@compaq.com * */#include <linux/config.h>	/* CONFIG_PROC_FS */#include <linux/module.h>#include <linux/version.h>#include <linux/types.h>#include <linux/pci.h>#include <linux/kernel.h>#include <linux/malloc.h>#include <linux/delay.h>#include <linux/major.h>#include <linux/fs.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 <asm/uaccess.h>#include <asm/io.h>#include <linux/blk.h>#include <linux/blkdev.h>#include <linux/genhd.h>#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))#define DRIVER_NAME "Compaq CISS Driver (v 2.4.0)"#define DRIVER_VERSION CCISS_DRIVER_VERSION(2,4,0)/* Embedded module documentation macros - see modules.h */MODULE_AUTHOR("Charles M. White III - Compaq Computer Corporation");MODULE_DESCRIPTION("Driver for Compaq Smart Array Controller 5300");#include "cciss_cmd.h"#include "cciss.h"#include <linux/cciss_ioctl.h>#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 },};/* How long to wait (in millesconds) for board to go into simple mode */#define MAX_CONFIG_WAIT 1000 #define READ_AHEAD 	 128#define NR_CMDS		 128 /* #commands that can be outstanding */#define MAX_CTLR 8static int nr_ctlr; static ctlr_info_t *hba[MAX_CTLR];static struct proc_dir_entry *proc_cciss;static void do_cciss_request(int i);/* * This is a hack.  This driver eats a major number for each controller, and * sets blkdev[xxx].request_fn to each one of these so the real request * function knows what controller its working with. */#define DO_CCISS_REQUEST(x) { do_cciss_request(x); }static void do_cciss_request0(request_queue_t * q) DO_CCISS_REQUEST(0);static void do_cciss_request1(request_queue_t * q) DO_CCISS_REQUEST(1);static void do_cciss_request2(request_queue_t * q) DO_CCISS_REQUEST(2);static void do_cciss_request3(request_queue_t * q) DO_CCISS_REQUEST(3);static void do_cciss_request4(request_queue_t * q) DO_CCISS_REQUEST(4);static void do_cciss_request5(request_queue_t * q) DO_CCISS_REQUEST(5);static void do_cciss_request6(request_queue_t * q) DO_CCISS_REQUEST(6);static void do_cciss_request7(request_queue_t * q) DO_CCISS_REQUEST(7);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(kdev_t dev);static int revalidate_logvol(kdev_t dev, int maxusage);static int frevalidate_logvol(kdev_t dev);static void cciss_getgeometry(int cntl_num);static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c);static void start_io( ctlr_info_t *h);#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 int cciss_proc_get_info(char *buffer, char **start, off_t offset, 		int length, int *eof, void *data) { return 0;}static void cciss_procinit(int i) {}#endif /* CONFIG_PROC_FS */static struct block_device_operations cciss_fops  = {	open:			cciss_open, 	release:        	cciss_release,        ioctl:			cciss_ioctl,	revalidate:		frevalidate_logvol,};/* * Report information about this controller. */#ifdef CONFIG_PROC_FSstatic 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;        ctlr = h->ctlr;        size = sprintf(buffer, "%s:  Compaq %s Controller\n"                "       Board ID: %08lx\n"		"       Firmware Version: %c%c%c%c\n"                "       Memory Address: %08lx\n"                "       IRQ: 0x%x\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 long)h->vaddr,                (unsigned int)h->intr,                h->num_luns,                 h->Qdepth, h->commands_outstanding,		h->maxQsinceinit, h->max_outstanding, h->maxSG);        pos += size; len += size;	for(i=0; i<h->num_luns; i++) {                drv = &h->drv[i];                size = sprintf(buffer+len, "cciss/c%dd%d: blksz=%d nr_blocks=%d\n",                                ctlr, i, drv->block_size, drv->nr_blocks);                pos += size; len += size;        }	size = sprintf(buffer+len, "nr_allocs = %d\nnr_frees = %d\n",                        h->nr_allocs, h->nr_frees);        pos += size; len += size;        *eof = 1;        *start = buffer+offset;        len -= offset;        if (len>length)                len = length;        return len;}/* * Get us a file in /proc/cciss that says something about each controller. * Create /proc/cciss if it doesn't exist yet. */static void __init cciss_procinit(int i){        if (proc_cciss == NULL) {                proc_cciss = proc_mkdir("driver/cciss", NULL);                if (!proc_cciss) 			return;        }        create_proc_read_entry(hba[i]->devname, 0, proc_cciss,        		cciss_proc_get_info, hba[i]);}#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 a NULL pointer.  * cmd_free() MUST be called with a NULL pointer if cmd_alloc was.  */ static CommandList_struct * cmd_alloc(ctlr_info_t *h){	CommandList_struct *c;	int i; 	u64bit temp64;	if (h == NULL)	{		c = (CommandList_struct *)kmalloc(sizeof(CommandList_struct), 			GFP_KERNEL);        	if(c==NULL)                 	return NULL;		memset(c, 0, sizeof(CommandList_struct));		c->err_info = (ErrorInfo_struct *)kmalloc(					sizeof(ErrorInfo_struct), GFP_KERNEL);			if (c->err_info == NULL)		{			kfree(c);			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%32, h->cmd_pool_bits+(i/32)) != 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));					c->err_info = h->errinfo_pool + i;		memset(c->err_info, 0, sizeof(ErrorInfo_struct));                h->nr_allocs++;        }	temp64.val = (__u64) virt_to_bus(c->err_info);	c->ErrDesc.Addr.lower = temp64.val32.lower;	c->ErrDesc.Addr.upper = temp64.val32.upper;	c->ErrDesc.Len = sizeof(ErrorInfo_struct);       	c->busaddr = virt_to_bus(c);        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 i;	if( h == NULL)	{ 		kfree(c->err_info);		kfree(c);		} else 	{		i = c - h->cmd_pool;		clear_bit(i%32, h->cmd_pool_bits+(i/32));                h->nr_frees++;        }}/*   * fills in the disk information.  */static void cciss_geninit( int ctlr){	drive_info_struct *drv;	int i,j;		/* Loop through each real device */ 	hba[ctlr]->gendisk.nr_real = 0; 	for(i=0; i< NWD; i++)	{		drv = &(hba[ctlr]->drv[i]);		if( !(drv->nr_blocks))			continue;		hba[ctlr]->hd[i << NWD_SHIFT].nr_sects = 		hba[ctlr]->sizes[i << NWD_SHIFT] = drv->nr_blocks;		/* for each partition */ 		for(j=0; j<MAX_PART; j++)		{			hba[ctlr]->blocksizes[(i<<NWD_SHIFT) + j] = 1024; 			hba[ctlr]->hardsizes[ (i<<NWD_SHIFT) + j] = 				drv->block_size;		}		hba[ctlr]->gendisk.nr_real++;	}}/* * Open.  Make sure the device is really there. */static int cciss_open(struct inode *inode, struct file *filep){	int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;	int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;#ifdef CCISS_DEBUG	printk(KERN_DEBUG "cciss_open %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);#endif /* CCISS_DEBUG */ 	if (ctlr > MAX_CTLR || hba[ctlr] == NULL)		return -ENXIO;	if (!suser() && hba[ctlr]->sizes[ MINOR(inode->i_rdev)] == 0)		return -ENXIO;	/*	 * Root is allowed to open raw volume zero even if its 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 (suser()		&& (hba[ctlr]->sizes[MINOR(inode->i_rdev)] == 0) 		&& (MINOR(inode->i_rdev)!= 0))		return -ENXIO;	hba[ctlr]->drv[dsk].usage_count++;	hba[ctlr]->usage_count++;	MOD_INC_USE_COUNT;	return 0;}/* * Close.  Sync first. */static int cciss_release(struct inode *inode, struct file *filep){	int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;	int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;#ifdef CCISS_DEBUG	printk(KERN_DEBUG "cciss_release %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);#endif /* CCISS_DEBUG */	/* fsync_dev(inode->i_rdev); */	hba[ctlr]->drv[dsk].usage_count--;	hba[ctlr]->usage_count--;	MOD_DEC_USE_COUNT;	return 0;}/* * ioctl  */static int cciss_ioctl(struct inode *inode, struct file *filep, 		unsigned int cmd, unsigned long arg){	int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;	int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;	int diskinfo[4];	struct hd_geometry *geo = (struct hd_geometry *)arg;#ifdef CCISS_DEBUG	printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);#endif /* CCISS_DEBUG */ 		switch(cmd) {	case HDIO_GETGEO:		if (hba[ctlr]->drv[dsk].cylinders) {			diskinfo[0] = hba[ctlr]->drv[dsk].heads;			diskinfo[1] = hba[ctlr]->drv[dsk].sectors;			diskinfo[2] = hba[ctlr]->drv[dsk].cylinders;		} else {			diskinfo[0] = 0xff;			diskinfo[1] = 0x3f;			diskinfo[2] = hba[ctlr]->drv[dsk].nr_blocks / (0xff*0x3f);		}		put_user(diskinfo[0], &geo->heads);		put_user(diskinfo[1], &geo->sectors);		put_user(diskinfo[2], &geo->cylinders);		put_user(hba[ctlr]->hd[MINOR(inode->i_rdev)].start_sect, &geo->start);		return 0;	case BLKGETSIZE:		if (!arg) return -EINVAL;		put_user(hba[ctlr]->hd[MINOR(inode->i_rdev)].nr_sects, (long*)arg);		return 0;	case BLKRRPART:		return revalidate_logvol(inode->i_rdev, 1);	case BLKFLSBUF:	case BLKROSET:	case BLKROGET:	case BLKRASET:	case BLKRAGET:	case BLKPG:		return( blk_ioctl(inode->i_rdev, cmd, arg));	case CCISS_GETPCIINFO:	{		cciss_pci_info_struct pciinfo;		if (!arg) return -EINVAL;		pciinfo.bus = hba[ctlr]->pci_bus;		pciinfo.dev_fn = hba[ctlr]->pci_dev_fn;		pciinfo.board_id = hba[ctlr]->board_id;		if (copy_to_user((void *) arg, &pciinfo,  sizeof( cciss_pci_info_struct )))			return  -EFAULT;		return(0);	}		case CCISS_GETINTINFO:	{		cciss_coalint_struct intinfo;		ctlr_info_t *c = hba[ctlr];		if (!arg) return -EINVAL;		intinfo.delay = readl(&c->cfgtable->HostWrite.CoalIntDelay);		intinfo.count = readl(&c->cfgtable->HostWrite.CoalIntCount);		if (copy_to_user((void *) arg, &intinfo, sizeof( cciss_coalint_struct )))			return -EFAULT;                return(0);        }	case CCISS_SETINTINFO:        {                cciss_coalint_struct intinfo;                ctlr_info_t *c = hba[ctlr];		unsigned long flags;		int i;		if (!arg) return -EINVAL;			if (!capable(CAP_SYS_ADMIN)) return -EPERM;		if (copy_from_user(&intinfo, (void *) arg, sizeof( cciss_coalint_struct)))			return -EFAULT;		if ( (intinfo.delay == 0 ) && (intinfo.count == 0))		{//			printk("cciss_ioctl: delay and count cannot be 0\n");			return( -EINVAL);		}		spin_lock_irqsave(&io_request_lock, flags);		/* Can only safely update if no commands outstanding */ 		if (c->commands_outstanding > 0 )		{//			printk("cciss_ioctl: cannot change coalasing "//				"%d commands outstanding on controller\n", //					c->commands_outstanding);			spin_unlock_irqrestore(&io_request_lock, flags);			return(-EINVAL);		}		/* Update the field, and then ring the doorbell */ 		writel( intinfo.delay, 			&(c->cfgtable->HostWrite.CoalIntDelay));		writel( intinfo.count,                         &(c->cfgtable->HostWrite.CoalIntCount));		writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);		for(i=0;i<MAX_CONFIG_WAIT;i++)		{			if (!(readl(c->vaddr + SA5_DOORBELL) 					& CFGTBL_ChangeReq))				break;			/* delay and try again */			udelay(1000);		}			spin_unlock_irqrestore(&io_request_lock, flags);		if (i >= MAX_CONFIG_WAIT)			return( -EFAULT);                return(0);        }	case CCISS_GETNODENAME:        {                NodeName_type NodeName;                ctlr_info_t *c = hba[ctlr];		int i; 		if (!arg) return -EINVAL;		for(i=0;i<16;i++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -