viodasd.c

来自「linux 内核源代码」· C语言 代码 · 共 822 行 · 第 1/2 页

C
822
字号
/* -*- linux-c -*- * viodasd.c *  Authors: Dave Boutcher <boutcher@us.ibm.com> *           Ryan Arnold <ryanarn@us.ibm.com> *           Colin Devilbiss <devilbis@us.ibm.com> *           Stephen Rothwell <sfr@au1.ibm.com> * * (C) Copyright 2000-2004 IBM 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.  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * This routine provides access to disk space (termed "DASD" in historical * IBM terms) owned and managed by an OS/400 partition running on the * same box as this Linux partition. * * All disk operations are performed by sending messages back and forth to * the OS/400 partition. */#include <linux/major.h>#include <linux/fs.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/blkdev.h>#include <linux/genhd.h>#include <linux/hdreg.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/string.h>#include <linux/dma-mapping.h>#include <linux/completion.h>#include <linux/device.h>#include <linux/scatterlist.h>#include <asm/uaccess.h>#include <asm/vio.h>#include <asm/iseries/hv_types.h>#include <asm/iseries/hv_lp_event.h>#include <asm/iseries/hv_lp_config.h>#include <asm/iseries/vio.h>#include <asm/firmware.h>MODULE_DESCRIPTION("iSeries Virtual DASD");MODULE_AUTHOR("Dave Boutcher");MODULE_LICENSE("GPL");/* * We only support 7 partitions per physical disk....so with minor * numbers 0-255 we get a maximum of 32 disks. */#define VIOD_GENHD_NAME		"iseries/vd"#define VIOD_VERS		"1.64"#define VIOD_KERN_WARNING	KERN_WARNING "viod: "#define VIOD_KERN_INFO		KERN_INFO "viod: "enum {	PARTITION_SHIFT = 3,	MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,	MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)};static DEFINE_SPINLOCK(viodasd_spinlock);#define VIOMAXREQ		16#define DEVICE_NO(cell)	((struct viodasd_device *)(cell) - &viodasd_devices[0])struct viodasd_waitevent {	struct completion	com;	int			rc;	u16			sub_result;	int			max_disk;	/* open */};static const struct vio_error_entry viodasd_err_table[] = {	{ 0x0201, EINVAL, "Invalid Range" },	{ 0x0202, EINVAL, "Invalid Token" },	{ 0x0203, EIO, "DMA Error" },	{ 0x0204, EIO, "Use Error" },	{ 0x0205, EIO, "Release Error" },	{ 0x0206, EINVAL, "Invalid Disk" },	{ 0x0207, EBUSY, "Cant Lock" },	{ 0x0208, EIO, "Already Locked" },	{ 0x0209, EIO, "Already Unlocked" },	{ 0x020A, EIO, "Invalid Arg" },	{ 0x020B, EIO, "Bad IFS File" },	{ 0x020C, EROFS, "Read Only Device" },	{ 0x02FF, EIO, "Internal Error" },	{ 0x0000, 0, NULL },};/* * Figure out the biggest I/O request (in sectors) we can accept */#define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)/* * Number of disk I/O requests we've sent to OS/400 */static int num_req_outstanding;/* * This is our internal structure for keeping track of disk devices */struct viodasd_device {	u16		cylinders;	u16		tracks;	u16		sectors;	u16		bytes_per_sector;	u64		size;	int		read_only;	spinlock_t	q_lock;	struct gendisk	*disk;	struct device	*dev;} viodasd_devices[MAX_DISKNO];/* * External open entry point. */static int viodasd_open(struct inode *ino, struct file *fil){	struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;	HvLpEvent_Rc hvrc;	struct viodasd_waitevent we;	u16 flags = 0;	if (d->read_only) {		if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))			return -EROFS;		flags = vioblockflags_ro;	}	init_completion(&we.com);	/* Send the open event to OS/400 */	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,			HvLpEvent_Type_VirtualIo,			viomajorsubtype_blockio | vioblockopen,			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,			viopath_sourceinst(viopath_hostLp),			viopath_targetinst(viopath_hostLp),			(u64)(unsigned long)&we, VIOVERSION << 16,			((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),			0, 0, 0);	if (hvrc != 0) {		printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);		return -EIO;	}	wait_for_completion(&we.com);	/* Check the return code */	if (we.rc != 0) {		const struct vio_error_entry *err =			vio_lookup_rc(viodasd_err_table, we.sub_result);		printk(VIOD_KERN_WARNING				"bad rc opening disk: %d:0x%04x (%s)\n",				(int)we.rc, we.sub_result, err->msg);		return -EIO;	}	return 0;}/* * External release entry point. */static int viodasd_release(struct inode *ino, struct file *fil){	struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;	HvLpEvent_Rc hvrc;	/* Send the event to OS/400.  We DON'T expect a response */	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,			HvLpEvent_Type_VirtualIo,			viomajorsubtype_blockio | vioblockclose,			HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,			viopath_sourceinst(viopath_hostLp),			viopath_targetinst(viopath_hostLp),			0, VIOVERSION << 16,			((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,			0, 0, 0);	if (hvrc != 0)		printk(VIOD_KERN_WARNING "HV close call failed %d\n",				(int)hvrc);	return 0;}/* External ioctl entry point. */static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo){	struct gendisk *disk = bdev->bd_disk;	struct viodasd_device *d = disk->private_data;	geo->sectors = d->sectors ? d->sectors : 32;	geo->heads = d->tracks ? d->tracks  : 64;	geo->cylinders = d->cylinders ? d->cylinders :		get_capacity(disk) / (geo->sectors * geo->heads);	return 0;}/* * Our file operations table */static struct block_device_operations viodasd_fops = {	.owner = THIS_MODULE,	.open = viodasd_open,	.release = viodasd_release,	.getgeo = viodasd_getgeo,};/* * End a request */static void viodasd_end_request(struct request *req, int uptodate,		int num_sectors){	if (end_that_request_first(req, uptodate, num_sectors))		return;	add_disk_randomness(req->rq_disk);	end_that_request_last(req, uptodate);}/* * Send an actual I/O request to OS/400 */static int send_request(struct request *req){	u64 start;	int direction;	int nsg;	u16 viocmd;	HvLpEvent_Rc hvrc;	struct vioblocklpevent *bevent;	struct HvLpEvent *hev;	struct scatterlist sg[VIOMAXBLOCKDMA];	int sgindex;	int statindex;	struct viodasd_device *d;	unsigned long flags;	start = (u64)req->sector << 9;	if (rq_data_dir(req) == READ) {		direction = DMA_FROM_DEVICE;		viocmd = viomajorsubtype_blockio | vioblockread;		statindex = 0;	} else {		direction = DMA_TO_DEVICE;		viocmd = viomajorsubtype_blockio | vioblockwrite;		statindex = 1;	}        d = req->rq_disk->private_data;	/* Now build the scatter-gather list */	sg_init_table(sg, VIOMAXBLOCKDMA);	nsg = blk_rq_map_sg(req->q, req, sg);	nsg = dma_map_sg(d->dev, sg, nsg, direction);	spin_lock_irqsave(&viodasd_spinlock, flags);	num_req_outstanding++;	/* This optimization handles a single DMA block */	if (nsg == 1)		hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,				HvLpEvent_Type_VirtualIo, viocmd,				HvLpEvent_AckInd_DoAck,				HvLpEvent_AckType_ImmediateAck,				viopath_sourceinst(viopath_hostLp),				viopath_targetinst(viopath_hostLp),				(u64)(unsigned long)req, VIOVERSION << 16,				((u64)DEVICE_NO(d) << 48), start,				((u64)sg_dma_address(&sg[0])) << 32,				sg_dma_len(&sg[0]));	else {		bevent = (struct vioblocklpevent *)			vio_get_event_buffer(viomajorsubtype_blockio);		if (bevent == NULL) {			printk(VIOD_KERN_WARNING			       "error allocating disk event buffer\n");			goto error_ret;		}		/*		 * Now build up the actual request.  Note that we store		 * the pointer to the request in the correlation		 * token so we can match the response up later		 */		memset(bevent, 0, sizeof(struct vioblocklpevent));		hev = &bevent->event;		hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |			HV_LP_EVENT_INT;		hev->xType = HvLpEvent_Type_VirtualIo;		hev->xSubtype = viocmd;		hev->xSourceLp = HvLpConfig_getLpIndex();		hev->xTargetLp = viopath_hostLp;		hev->xSizeMinus1 =			offsetof(struct vioblocklpevent, u.rw_data.dma_info) +			(sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;		hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);		hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);		hev->xCorrelationToken = (u64)req;		bevent->version = VIOVERSION;		bevent->disk = DEVICE_NO(d);		bevent->u.rw_data.offset = start;		/*		 * Copy just the dma information from the sg list		 * into the request		 */		for (sgindex = 0; sgindex < nsg; sgindex++) {			bevent->u.rw_data.dma_info[sgindex].token =				sg_dma_address(&sg[sgindex]);			bevent->u.rw_data.dma_info[sgindex].len =				sg_dma_len(&sg[sgindex]);		}		/* Send the request */		hvrc = HvCallEvent_signalLpEvent(&bevent->event);		vio_free_event_buffer(viomajorsubtype_blockio, bevent);	}	if (hvrc != HvLpEvent_Rc_Good) {		printk(VIOD_KERN_WARNING		       "error sending disk event to OS/400 (rc %d)\n",		       (int)hvrc);		goto error_ret;	}	spin_unlock_irqrestore(&viodasd_spinlock, flags);	return 0;error_ret:	num_req_outstanding--;	spin_unlock_irqrestore(&viodasd_spinlock, flags);	dma_unmap_sg(d->dev, sg, nsg, direction);	return -1;}/* * This is the external request processing routine */static void do_viodasd_request(struct request_queue *q){	struct request *req;	/*	 * If we already have the maximum number of requests	 * outstanding to OS/400 just bail out. We'll come	 * back later.	 */	while (num_req_outstanding < VIOMAXREQ) {		req = elv_next_request(q);		if (req == NULL)			return;		/* dequeue the current request from the queue */		blkdev_dequeue_request(req);		/* check that request contains a valid command */		if (!blk_fs_request(req)) {			viodasd_end_request(req, 0, req->hard_nr_sectors);			continue;		}		/* Try sending the request */		if (send_request(req) != 0)			viodasd_end_request(req, 0, req->hard_nr_sectors);	}}/* * Probe a single disk and fill in the viodasd_device structure * for it. */static int probe_disk(struct viodasd_device *d){	HvLpEvent_Rc hvrc;	struct viodasd_waitevent we;	int dev_no = DEVICE_NO(d);	struct gendisk *g;	struct request_queue *q;	u16 flags = 0;retry:	init_completion(&we.com);	/* Send the open event to OS/400 */	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,			HvLpEvent_Type_VirtualIo,			viomajorsubtype_blockio | vioblockopen,			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,			viopath_sourceinst(viopath_hostLp),			viopath_targetinst(viopath_hostLp),			(u64)(unsigned long)&we, VIOVERSION << 16,			((u64)dev_no << 48) | ((u64)flags<< 32),			0, 0, 0);

⌨️ 快捷键说明

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