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

📄 cciss_scsi.c

📁 Linux块设备驱动源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *    Disk Array driver for Compaq SA53xx Controllers, SCSI Tape module *    Copyright 2001 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 iss_storagedev@hp.com *     *    Author: Stephen M. Cameron */#ifdef CONFIG_CISS_SCSI_TAPE/* Here we have code to present the driver as a scsi driver    as it is simultaneously presented as a block driver.  The    reason for doing this is to allow access to SCSI tape drives   through the array controller.  Note in particular, neither    physical nor logical disks are presented through the scsi layer. */#include <scsi/scsi.h> #include <scsi/scsi_cmnd.h>#include <scsi/scsi_device.h>#include <scsi/scsi_host.h> #include <asm/atomic.h>#include <linux/timer.h>#include <linux/completion.h>#include "cciss_scsi.h"/* some prototypes... */ static int sendcmd(	__u8	cmd,	int	ctlr,	void	*buff,	size_t	size,	unsigned int use_unit_num, /* 0: address the controller,				      1: address logical volume log_unit, 				      2: address is in scsi3addr */	unsigned int log_unit,	__u8	page_code,	unsigned char *scsi3addr,	int cmd_type);static int cciss_scsi_proc_info(		struct Scsi_Host *sh,		char *buffer, /* data buffer */		char **start, 	   /* where data in buffer starts */		off_t offset,	   /* offset from start of imaginary file */		int length, 	   /* length of data in buffer */		int func);	   /* 0 == read, 1 == write */static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,		void (* done)(struct scsi_cmnd *));static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {	{ .name = "cciss0", .ndevices = 0 },	{ .name = "cciss1", .ndevices = 0 },	{ .name = "cciss2", .ndevices = 0 },	{ .name = "cciss3", .ndevices = 0 },	{ .name = "cciss4", .ndevices = 0 },	{ .name = "cciss5", .ndevices = 0 },	{ .name = "cciss6", .ndevices = 0 },	{ .name = "cciss7", .ndevices = 0 },};static struct scsi_host_template cciss_driver_template = {	.module			= THIS_MODULE,	.name			= "cciss",	.proc_name		= "cciss",	.proc_info		= cciss_scsi_proc_info,	.queuecommand		= cciss_scsi_queue_command,	.can_queue		= SCSI_CCISS_CAN_QUEUE,	.this_id		= 7,	.sg_tablesize		= MAXSGENTRIES,	.cmd_per_lun		= 1,	.use_clustering		= DISABLE_CLUSTERING,};#pragma pack(1)struct cciss_scsi_cmd_stack_elem_t {	CommandList_struct cmd;	ErrorInfo_struct Err;	__u32 busaddr;	__u32 pad;};#pragma pack()#define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \		CCISS_MAX_SCSI_DEVS_PER_HBA + 2)			// plus two for init time usage#pragma pack(1)struct cciss_scsi_cmd_stack_t {	struct cciss_scsi_cmd_stack_elem_t *pool;	struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];	dma_addr_t cmd_pool_handle;	int top;};#pragma pack()struct cciss_scsi_adapter_data_t {	struct Scsi_Host *scsi_host;	struct cciss_scsi_cmd_stack_t cmd_stack;	int registered;	spinlock_t lock; // to protect ccissscsi[ctlr]; };#define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \	&(((struct cciss_scsi_adapter_data_t *) \	hba[ctlr]->scsi_ctlr)->lock), flags);#define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \	&(((struct cciss_scsi_adapter_data_t *) \	hba[ctlr]->scsi_ctlr)->lock), flags);static CommandList_struct *scsi_cmd_alloc(ctlr_info_t *h){	/* assume only one process in here at a time, locking done by caller. */	/* use CCISS_LOCK(ctlr) */	/* might be better to rewrite how we allocate scsi commands in a way that */	/* needs no locking at all. */	/* take the top memory chunk off the stack and return it, if any. */	struct cciss_scsi_cmd_stack_elem_t *c;	struct cciss_scsi_adapter_data_t *sa;	struct cciss_scsi_cmd_stack_t *stk;	u64bit temp64;	sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;	stk = &sa->cmd_stack; 	if (stk->top < 0) 		return NULL;	c = stk->elem[stk->top]; 		/* memset(c, 0, sizeof(*c)); */	memset(&c->cmd, 0, sizeof(c->cmd));	memset(&c->Err, 0, sizeof(c->Err));	/* set physical addr of cmd and addr of scsi parameters */	c->cmd.busaddr = c->busaddr; 	/* (__u32) (stk->cmd_pool_handle + 		(sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */	temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));	/* (__u64) (stk->cmd_pool_handle + 		(sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +		 sizeof(CommandList_struct)); */	stk->top--;	c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;	c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;	c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);		c->cmd.ctlr = h->ctlr;	c->cmd.err_info = &c->Err;	return (CommandList_struct *) c;}static void scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd){	/* assume only one process in here at a time, locking done by caller. */	/* use CCISS_LOCK(ctlr) */	/* drop the free memory chunk on top of the stack. */	struct cciss_scsi_adapter_data_t *sa;	struct cciss_scsi_cmd_stack_t *stk;	sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;	stk = &sa->cmd_stack; 	if (stk->top >= CMD_STACK_SIZE) {		printk("cciss: scsi_cmd_free called too many times.\n");		BUG();	}	stk->top++;	stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;}static intscsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa){	int i;	struct cciss_scsi_cmd_stack_t *stk;	size_t size;	stk = &sa->cmd_stack; 	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;	// pci_alloc_consistent guarantees 32-bit DMA address will	// be used	stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)		pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);	if (stk->pool == NULL) {		printk("stk->pool is null\n");		return -1;	}	for (i=0; i<CMD_STACK_SIZE; i++) {		stk->elem[i] = &stk->pool[i];		stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle + 			(sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));	}	stk->top = CMD_STACK_SIZE-1;	return 0;}static voidscsi_cmd_stack_free(int ctlr){	struct cciss_scsi_adapter_data_t *sa;	struct cciss_scsi_cmd_stack_t *stk;	size_t size;	sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;	stk = &sa->cmd_stack; 	if (stk->top != CMD_STACK_SIZE-1) {		printk( "cciss: %d scsi commands are still outstanding.\n",			CMD_STACK_SIZE - stk->top);		// BUG();		printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);	}	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;	pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);	stk->pool = NULL;}/* scsi_device_types comes from scsi.h */#define DEVICETYPE(n) (n<0 || n>MAX_SCSI_DEVICE_CODE) ? \	"Unknown" : scsi_device_types[n]#if 0static int xmargin=8;static int amargin=60;static voidprint_bytes (unsigned char *c, int len, int hex, int ascii){	int i;	unsigned char *x;	if (hex)	{		x = c;		for (i=0;i<len;i++)		{			if ((i % xmargin) == 0 && i>0) printk("\n");			if ((i % xmargin) == 0) printk("0x%04x:", i);			printk(" %02x", *x);			x++;		}		printk("\n");	}	if (ascii)	{		x = c;		for (i=0;i<len;i++)		{			if ((i % amargin) == 0 && i>0) printk("\n");			if ((i % amargin) == 0) printk("0x%04x:", i);			if (*x > 26 && *x < 128) printk("%c", *x);			else printk(".");			x++;		}		printk("\n");	}}static voidprint_cmd(CommandList_struct *cp){	printk("queue:%d\n", cp->Header.ReplyQueue);	printk("sglist:%d\n", cp->Header.SGList);	printk("sgtot:%d\n", cp->Header.SGTotal);	printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper, 			cp->Header.Tag.lower);	printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",		cp->Header.LUN.LunAddrBytes[0],		cp->Header.LUN.LunAddrBytes[1],		cp->Header.LUN.LunAddrBytes[2],		cp->Header.LUN.LunAddrBytes[3],		cp->Header.LUN.LunAddrBytes[4],		cp->Header.LUN.LunAddrBytes[5],		cp->Header.LUN.LunAddrBytes[6],		cp->Header.LUN.LunAddrBytes[7]);	printk("CDBLen:%d\n", cp->Request.CDBLen);	printk("Type:%d\n",cp->Request.Type.Type);	printk("Attr:%d\n",cp->Request.Type.Attribute);	printk(" Dir:%d\n",cp->Request.Type.Direction);	printk("Timeout:%d\n",cp->Request.Timeout);	printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"		" %02x %02x %02x %02x %02x %02x %02x %02x\n",		cp->Request.CDB[0], cp->Request.CDB[1],		cp->Request.CDB[2], cp->Request.CDB[3],		cp->Request.CDB[4], cp->Request.CDB[5],		cp->Request.CDB[6], cp->Request.CDB[7],		cp->Request.CDB[8], cp->Request.CDB[9],		cp->Request.CDB[10], cp->Request.CDB[11],		cp->Request.CDB[12], cp->Request.CDB[13],		cp->Request.CDB[14], cp->Request.CDB[15]),	printk("edesc.Addr: 0x%08x/0%08x, Len  = %d\n", 		cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower, 			cp->ErrDesc.Len);	printk("sgs..........Errorinfo:\n");	printk("scsistatus:%d\n", cp->err_info->ScsiStatus);	printk("senselen:%d\n", cp->err_info->SenseLen);	printk("cmd status:%d\n", cp->err_info->CommandStatus);	printk("resid cnt:%d\n", cp->err_info->ResidualCnt);	printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);	printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);	printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);			}#endifstatic int find_bus_target_lun(int ctlr, int *bus, int *target, int *lun){	/* finds an unused bus, target, lun for a new device */	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 	int i, found=0;	unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];	memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);	target_taken[SELF_SCSI_ID] = 1;		for (i=0;i<ccissscsi[ctlr].ndevices;i++)		target_taken[ccissscsi[ctlr].dev[i].target] = 1;		for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {		if (!target_taken[i]) {			*bus = 0; *target=i; *lun = 0; found=1;			break;		}	}	return (!found);	}static int cciss_scsi_add_entry(int ctlr, int hostno, 		unsigned char *scsi3addr, int devtype){	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 	int n = ccissscsi[ctlr].ndevices;	struct cciss_scsi_dev_t *sd;	if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {		printk("cciss%d: Too many devices, "			"some will be inaccessible.\n", ctlr);		return -1;	}	sd = &ccissscsi[ctlr].dev[n];	if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)		return -1;	memcpy(&sd->scsi3addr[0], scsi3addr, 8);	sd->devtype = devtype;	ccissscsi[ctlr].ndevices++;	/* initially, (before registering with scsi layer) we don't 	   know our hostno and we don't want to print anything first 	   time anyway (the scsi layer's inquiries will show that info) */	if (hostno != -1)		printk("cciss%d: %s device c%db%dt%dl%d added.\n", 			ctlr, DEVICETYPE(sd->devtype), hostno, 			sd->bus, sd->target, sd->lun);	return 0;}static voidcciss_scsi_remove_entry(int ctlr, int hostno, int entry){	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 	int i;	struct cciss_scsi_dev_t sd;	if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;	sd = ccissscsi[ctlr].dev[entry];	for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)		ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];	ccissscsi[ctlr].ndevices--;	printk("cciss%d: %s device c%db%dt%dl%d removed.\n",		ctlr, DEVICETYPE(sd.devtype), hostno, 			sd.bus, sd.target, sd.lun);}#define SCSI3ADDR_EQ(a,b) ( \	(a)[7] == (b)[7] && \	(a)[6] == (b)[6] && \	(a)[5] == (b)[5] && \	(a)[4] == (b)[4] && \	(a)[3] == (b)[3] && \	(a)[2] == (b)[2] && \	(a)[1] == (b)[1] && \	(a)[0] == (b)[0])static intadjust_cciss_scsi_table(int ctlr, int hostno,	struct cciss_scsi_dev_t sd[], int nsds){	/* sd contains scsi3 addresses and devtypes, but	   bus target and lun are not filled in.  This funciton	   takes what's in sd to be the current and adjusts	   ccissscsi[] to be in line with what's in sd. */ 	int i,j, found, changes=0;	struct cciss_scsi_dev_t *csd;	unsigned long flags;	CPQ_TAPE_LOCK(ctlr, flags);	/* find any devices in ccissscsi[] that are not in 	   sd[] and remove them from ccissscsi[] */	i = 0;	while(i<ccissscsi[ctlr].ndevices) {		csd = &ccissscsi[ctlr].dev[i];		found=0;		for (j=0;j<nsds;j++) {			if (SCSI3ADDR_EQ(sd[j].scsi3addr,				csd->scsi3addr)) {				if (sd[j].devtype == csd->devtype)					found=2;				else					found=1;				break;			}		}		if (found == 0) { /* device no longer present. */ 			changes++;			/* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",				ctlr, DEVICETYPE(csd->devtype), hostno, 					csd->bus, csd->target, csd->lun); */			cciss_scsi_remove_entry(ctlr, hostno, i);			/* note, i not incremented */		} 		else if (found == 1) { /* device is different kind */			changes++;			printk("cciss%d: device c%db%dt%dl%d type changed "				"(device type now %s).\n",				ctlr, hostno, csd->bus, csd->target, csd->lun,					DEVICETYPE(csd->devtype));			csd->devtype = sd[j].devtype;			i++;	/* so just move along. */		} else 		/* device is same as it ever was, */			i++;	/* so just move along. */	}	/* Now, make sure every device listed in sd[] is also 	   listed in ccissscsi[], adding them if they aren't found */	for (i=0;i<nsds;i++) {		found=0;		for (j=0;j<ccissscsi[ctlr].ndevices;j++) {			csd = &ccissscsi[ctlr].dev[j];			if (SCSI3ADDR_EQ(sd[i].scsi3addr,				csd->scsi3addr)) {				if (sd[i].devtype == csd->devtype)					found=2;	/* found device */				else					found=1; 	/* found a bug. */				break;			}		}		if (!found) {			changes++;			if (cciss_scsi_add_entry(ctlr, hostno, 				&sd[i].scsi3addr[0], sd[i].devtype) != 0)				break;

⌨️ 快捷键说明

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