i2o_scsi.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 883 行 · 第 1/2 页

C
883
字号
/* * 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, 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.  See the GNU * General Public License for more details. * * For the avoidance of doubt the "preferred form" of this code is one which * is in an open non patent encumbered format. Where cryptographic key signing * forms part of the process of creating an executable the information * including keys needed to generate an equivalently functional executable * are deemed to be part of the source code. * *  Complications for I2O scsi * *	o	Each (bus,lun) is a logical device in I2O. We keep a map *		table. We spoof failed selection for unmapped units *	o	Request sense buffers can come back for free. *	o	Scatter gather is a bit dynamic. We have to investigate at *		setup time. *	o	Some of our resources are dynamically shared. The i2o core *		needs a message reservation protocol to avoid swap v net *		deadlocking. We need to back off queue requests. * *	In general the firmware wants to help. Where its help isn't performance *	useful we just ignore the aid. Its not worth the code in truth. * * Fixes/additions: *	Steve Ralston: *		Scatter gather now works *	Markus Lidel <Markus.Lidel@shadowconnect.com>: *		Minor fixes for 2.6. * * To Do: *	64bit cleanups *	Fix the resource management problems. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/string.h>#include <linux/ioport.h>#include <linux/jiffies.h>#include <linux/interrupt.h>#include <linux/timer.h>#include <linux/delay.h>#include <linux/proc_fs.h>#include <linux/prefetch.h>#include <linux/pci.h>#include <linux/blkdev.h>#include <linux/i2o.h>#include <asm/dma.h>#include <asm/system.h>#include <asm/io.h>#include <asm/atomic.h>#include <scsi/scsi.h>#include <scsi/scsi_host.h>#include <scsi/scsi_device.h>#include <scsi/scsi_cmnd.h>#define VERSION_STRING        "Version 0.1.2"static struct i2o_driver i2o_scsi_driver;static int i2o_scsi_max_id = 16;static int i2o_scsi_max_lun = 8;struct i2o_scsi_host {	struct Scsi_Host *scsi_host;	/* pointer to the SCSI host */	struct i2o_controller *iop;	/* pointer to the I2O controller */	struct i2o_device *channel[0];	/* channel->i2o_dev mapping table */};static struct scsi_host_template i2o_scsi_host_template;#define I2O_SCSI_CAN_QUEUE	4/* SCSI OSM class handling definition */static struct i2o_class_id i2o_scsi_class_id[] = {	{I2O_CLASS_SCSI_PERIPHERAL},	{I2O_CLASS_END}};static struct i2o_scsi_host *i2o_scsi_host_alloc(struct i2o_controller *c){	struct i2o_scsi_host *i2o_shost;	struct i2o_device *i2o_dev;	struct Scsi_Host *scsi_host;	int max_channel = 0;	u8 type;	int i;	size_t size;	i2o_status_block *sb;	list_for_each_entry(i2o_dev, &c->devices, list)	    if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {		if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1))	/* SCSI bus */			max_channel++;	}	if (!max_channel) {		printk(KERN_WARNING "scsi-osm: no channels found on %s\n",		       c->name);		return ERR_PTR(-EFAULT);	}	size = max_channel * sizeof(struct i2o_device *)	    + sizeof(struct i2o_scsi_host);	scsi_host = scsi_host_alloc(&i2o_scsi_host_template, size);	if (!scsi_host) {		printk(KERN_WARNING "scsi-osm: Could not allocate SCSI host\n");		return ERR_PTR(-ENOMEM);	}	scsi_host->max_channel = max_channel - 1;	scsi_host->max_id = i2o_scsi_max_id;	scsi_host->max_lun = i2o_scsi_max_lun;	scsi_host->this_id = c->unit;	sb = c->status_block.virt;	scsi_host->sg_tablesize = (sb->inbound_frame_size -				   sizeof(struct i2o_message) / 4 - 6) / 2;	i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;	i2o_shost->scsi_host = scsi_host;	i2o_shost->iop = c;	i = 0;	list_for_each_entry(i2o_dev, &c->devices, list)	    if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {		if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1))	/* only SCSI bus */			i2o_shost->channel[i++] = i2o_dev;		if (i >= max_channel)			break;	}	return i2o_shost;};/** *	i2o_scsi_get_host - Get an I2O SCSI host *	@c: I2O controller to for which to get the SCSI host * *	If the I2O controller already exists as SCSI host, the SCSI host *	is returned, otherwise the I2O controller is added to the SCSI *	core. * *	Returns pointer to the I2O SCSI host on success or NULL on failure. */static struct i2o_scsi_host *i2o_scsi_get_host(struct i2o_controller *c){	return c->driver_data[i2o_scsi_driver.context];};/** *	i2o_scsi_remove - Remove I2O device from SCSI core *	@dev: device which should be removed * *	Removes the I2O device from the SCSI core again. * *	Returns 0 on success. */static int i2o_scsi_remove(struct device *dev){	struct i2o_device *i2o_dev = to_i2o_device(dev);	struct i2o_controller *c = i2o_dev->iop;	struct i2o_scsi_host *i2o_shost;	struct scsi_device *scsi_dev;	i2o_shost = i2o_scsi_get_host(c);	shost_for_each_device(scsi_dev, i2o_shost->scsi_host)	    if (scsi_dev->hostdata == i2o_dev) {		scsi_remove_device(scsi_dev);		scsi_device_put(scsi_dev);		break;	}	return 0;};/** *	i2o_scsi_probe - verify if dev is a I2O SCSI device and install it *	@dev: device to verify if it is a I2O SCSI device * *	Retrieve channel, id and lun for I2O device. If everthing goes well *	register the I2O device as SCSI device on the I2O SCSI controller. * *	Returns 0 on success or negative error code on failure. */static int i2o_scsi_probe(struct device *dev){	struct i2o_device *i2o_dev = to_i2o_device(dev);	struct i2o_controller *c = i2o_dev->iop;	struct i2o_scsi_host *i2o_shost;	struct Scsi_Host *scsi_host;	struct i2o_device *parent;	struct scsi_device *scsi_dev;	u32 id;	u64 lun;	int channel = -1;	int i;	i2o_shost = i2o_scsi_get_host(c);	if (!i2o_shost)		return -EFAULT;	scsi_host = i2o_shost->scsi_host;	if (i2o_parm_field_get(i2o_dev, 0, 3, &id, 4) < 0)		return -EFAULT;	if (id >= scsi_host->max_id) {		printk(KERN_WARNING "scsi-osm: SCSI device id (%d) >= max_id "		       "of I2O host (%d)", id, scsi_host->max_id);		return -EFAULT;	}	if (i2o_parm_field_get(i2o_dev, 0, 4, &lun, 8) < 0)		return -EFAULT;	if (lun >= scsi_host->max_lun) {		printk(KERN_WARNING "scsi-osm: SCSI device id (%d) >= max_lun "		       "of I2O host (%d)", (unsigned int)lun,		       scsi_host->max_lun);		return -EFAULT;	}	parent = i2o_iop_find_device(c, i2o_dev->lct_data.parent_tid);	if (!parent) {		printk(KERN_WARNING "scsi-osm: can not find parent of device "		       "%03x\n", i2o_dev->lct_data.tid);		return -EFAULT;	}	for (i = 0; i <= i2o_shost->scsi_host->max_channel; i++)		if (i2o_shost->channel[i] == parent)			channel = i;	if (channel == -1) {		printk(KERN_WARNING "scsi-osm: can not find channel of device "		       "%03x\n", i2o_dev->lct_data.tid);		return -EFAULT;	}	scsi_dev =	    __scsi_add_device(i2o_shost->scsi_host, channel, id, lun, i2o_dev);	if (!scsi_dev) {		printk(KERN_WARNING "scsi-osm: can not add SCSI device "		       "%03x\n", i2o_dev->lct_data.tid);		return -EFAULT;	}	pr_debug("Added new SCSI device %03x (cannel: %d, id: %d, lun: %d)\n",		 i2o_dev->lct_data.tid, channel, id, (unsigned int)lun);	return 0;};static const char *i2o_scsi_info(struct Scsi_Host *SChost){	struct i2o_scsi_host *hostdata;	hostdata = (struct i2o_scsi_host *)SChost->hostdata;	return hostdata->iop->name;}#if 0/** *	i2o_retry_run		-	retry on timeout *	@f: unused * *	Retry congested frames. This actually needs pushing down into *	i2o core. We should only bother the OSM with this when we can't *	queue and retry the frame. Or perhaps we should call the OSM *	and its default handler should be this in the core, and this *	call a 2nd "I give up" handler in the OSM ? */static void i2o_retry_run(unsigned long f){	int i;	unsigned long flags;	spin_lock_irqsave(&retry_lock, flags);	for (i = 0; i < retry_ct; i++)		i2o_post_message(retry_ctrl[i], virt_to_bus(retry[i]));	retry_ct = 0;	spin_unlock_irqrestore(&retry_lock, flags);}/** *	flush_pending		-	empty the retry queue * *	Turn each of the pending commands into a NOP and post it back *	to the controller to clear it. */static void flush_pending(void){	int i;	unsigned long flags;	spin_lock_irqsave(&retry_lock, flags);	for (i = 0; i < retry_ct; i++) {		retry[i][0] &= ~0xFFFFFF;		retry[i][0] |= I2O_CMD_UTIL_NOP << 24;		i2o_post_message(retry_ctrl[i], virt_to_bus(retry[i]));	}	retry_ct = 0;	spin_unlock_irqrestore(&retry_lock, flags);}#endif/** *	i2o_scsi_reply - SCSI OSM message reply handler *	@c: controller issuing the reply *	@m: message id for flushing *	@msg: the message from the controller * *	Process reply messages (interrupts in normal scsi controller think). *	We can get a variety of messages to process. The normal path is *	scsi command completions. We must also deal with IOP failures, *	the reply to a bus reset and the reply to a LUN query. * *	Returns 0 on success and if the reply should not be flushed or > 0 *	on success and if the reply should be flushed. Returns negative error *	code on failure and if the reply should be flushed. */static int i2o_scsi_reply(struct i2o_controller *c, u32 m,			  struct i2o_message *msg){	struct scsi_cmnd *cmd;	struct device *dev;	u8 as, ds, st;	cmd = i2o_cntxt_list_get(c, readl(&msg->u.s.tcntxt));	if (msg->u.head[0] & (1 << 13)) {		struct i2o_message *pmsg;	/* preserved message */		u32 pm;		pm = readl(&msg->body[3]);		pmsg = c->in_queue.virt + pm;		printk("IOP fail.\n");		printk("From %d To %d Cmd %d.\n",		       (msg->u.head[1] >> 12) & 0xFFF,		       msg->u.head[1] & 0xFFF, msg->u.head[1] >> 24);		printk("Failure Code %d.\n", msg->body[0] >> 24);		if (msg->body[0] & (1 << 16))			printk("Format error.\n");		if (msg->body[0] & (1 << 17))			printk("Path error.\n");		if (msg->body[0] & (1 << 18))			printk("Path State.\n");		if (msg->body[0] & (1 << 18))			printk("Congestion.\n");		printk("Failing message is %p.\n", pmsg);		cmd = i2o_cntxt_list_get(c, readl(&pmsg->u.s.tcntxt));		if (!cmd)			return 1;		printk("Aborted %ld\n", cmd->serial_number);		cmd->result = DID_ERROR << 16;		cmd->scsi_done(cmd);		/* Now flush the message by making it a NOP */		i2o_msg_nop(c, pm);		return 1;	}	/*	 *      Low byte is device status, next is adapter status,	 *      (then one byte reserved), then request status.	 */	ds = (u8) readl(&msg->body[0]);	as = (u8) (readl(&msg->body[0]) >> 8);	st = (u8) (readl(&msg->body[0]) >> 24);	/*	 *      Is this a control request coming back - eg an abort ?	 */	if (!cmd) {		if (st)			printk(KERN_WARNING "SCSI abort: %08X",			       readl(&msg->body[0]));		printk(KERN_INFO "SCSI abort completed.\n");		return -EFAULT;	}	pr_debug("Completed %ld\n", cmd->serial_number);	if (st) {		u32 count, error;		/* An error has occurred */		switch (st) {		case 0x06:			count = readl(&msg->body[1]);			if (count < cmd->underflow) {				int i;				printk(KERN_ERR "SCSI: underflow 0x%08X 0x%08X"				       "\n", count, cmd->underflow);				printk("Cmd: ");				for (i = 0; i < 15; i++)					printk("%02X ", cmd->cmnd[i]);				printk(".\n");				cmd->result = (DID_ERROR << 16);			}			break;		default:			error = readl(&msg->body[0]);			printk(KERN_ERR "scsi-osm: SCSI error %08x\n", error);			if ((error & 0xff) == 0x02 /*CHECK_CONDITION */ ) {				int i;				u32 len = sizeof(cmd->sense_buffer);				len = (len > 40) ? 40 : len;				// Copy over the sense data				memcpy(cmd->sense_buffer, (void *)&msg->body[3],				       len);				for (i = 0; i <= len; i++)					printk(KERN_INFO "%02x\n",					       cmd->sense_buffer[i]);				if (cmd->sense_buffer[0] == 0x70

⌨️ 快捷键说明

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