xref: /linux/drivers/scsi/sg.c (revision e915e872ed921d707bc32b3f2184d43abfa8c9e2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  History:
31da177e4SLinus Torvalds  *  Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
41da177e4SLinus Torvalds  *           to allow user process control of SCSI devices.
51da177e4SLinus Torvalds  *  Development Sponsored by Killy Corp. NY NY
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Original driver (sg.c):
81da177e4SLinus Torvalds  *        Copyright (C) 1992 Lawrence Foard
91da177e4SLinus Torvalds  * Version 2 and 3 extensions to driver:
101da177e4SLinus Torvalds  *        Copyright (C) 1998 - 2005 Douglas Gilbert
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Modified  19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Devfs support
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
151da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
161da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
171da177e4SLinus Torvalds  * any later version.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21b2155d04SDouglas Gilbert static int sg_version_num = 30534;	/* 2 digits for each component */
22b2155d04SDouglas Gilbert #define SG_VERSION_STR "3.5.34"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /*
251da177e4SLinus Torvalds  *  D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
261da177e4SLinus Torvalds  *      - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
271da177e4SLinus Torvalds  *        the kernel/module needs to be built with CONFIG_SCSI_LOGGING
281da177e4SLinus Torvalds  *        (otherwise the macros compile to empty statements).
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds #include <linux/module.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <linux/fs.h>
341da177e4SLinus Torvalds #include <linux/kernel.h>
351da177e4SLinus Torvalds #include <linux/sched.h>
361da177e4SLinus Torvalds #include <linux/string.h>
371da177e4SLinus Torvalds #include <linux/mm.h>
381da177e4SLinus Torvalds #include <linux/errno.h>
391da177e4SLinus Torvalds #include <linux/mtio.h>
401da177e4SLinus Torvalds #include <linux/ioctl.h>
411da177e4SLinus Torvalds #include <linux/fcntl.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds #include <linux/poll.h>
441da177e4SLinus Torvalds #include <linux/moduleparam.h>
451da177e4SLinus Torvalds #include <linux/cdev.h>
467c07d613SJames Bottomley #include <linux/idr.h>
471da177e4SLinus Torvalds #include <linux/seq_file.h>
481da177e4SLinus Torvalds #include <linux/blkdev.h>
491da177e4SLinus Torvalds #include <linux/delay.h>
506da127adSChristof Schmitt #include <linux/blktrace_api.h>
51eb09d3d4SJonathan Corbet #include <linux/smp_lock.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include "scsi.h"
54db9dff36S #include <scsi/scsi_dbg.h>
551da177e4SLinus Torvalds #include <scsi/scsi_host.h>
561da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
571da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>
581da177e4SLinus Torvalds #include <scsi/sg.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include "scsi_logging.h"
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
631da177e4SLinus Torvalds #include <linux/proc_fs.h>
647ca63cb4SDouglas Gilbert static char *sg_version_date = "20061027";
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static int sg_proc_init(void);
671da177e4SLinus Torvalds static void sg_proc_cleanup(void);
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define SG_ALLOW_DIO_DEF 0
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define SG_MAX_DEVS 32768
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
761da177e4SLinus Torvalds  * Then when using 32 bit integers x * m may overflow during the calculation.
771da177e4SLinus Torvalds  * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
781da177e4SLinus Torvalds  * calculates the same, but prevents the overflow when both m and d
791da177e4SLinus Torvalds  * are "small" numbers (like HZ and USER_HZ).
801da177e4SLinus Torvalds  * Of course an overflow is inavoidable if the result of muldiv doesn't fit
811da177e4SLinus Torvalds  * in 32 bits.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds int sg_big_buff = SG_DEF_RESERVED_SIZE;
881da177e4SLinus Torvalds /* N.B. This variable is readable and writeable via
891da177e4SLinus Torvalds    /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
901da177e4SLinus Torvalds    of this size (or less if there is not enough memory) will be reserved
911da177e4SLinus Torvalds    for use by this file descriptor. [Deprecated usage: this variable is also
921da177e4SLinus Torvalds    readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
931da177e4SLinus Torvalds    the kernel (i.e. it is not a module).] */
941da177e4SLinus Torvalds static int def_reserved_size = -1;	/* picks up init parameter */
951da177e4SLinus Torvalds static int sg_allow_dio = SG_ALLOW_DIO_DEF;
961da177e4SLinus Torvalds 
976460e75aSDouglas Gilbert static int scatter_elem_sz = SG_SCATTER_SZ;
986460e75aSDouglas Gilbert static int scatter_elem_sz_prev = SG_SCATTER_SZ;
996460e75aSDouglas Gilbert 
1001da177e4SLinus Torvalds #define SG_SECTOR_SZ 512
1011da177e4SLinus Torvalds #define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
1021da177e4SLinus Torvalds 
103ee959b00STony Jones static int sg_add(struct device *, struct class_interface *);
104ee959b00STony Jones static void sg_remove(struct device *, struct class_interface *);
1051da177e4SLinus Torvalds 
1067c07d613SJames Bottomley static DEFINE_IDR(sg_index_idr);
1077c07d613SJames Bottomley static DEFINE_RWLOCK(sg_index_lock);	/* Also used to lock
1081da177e4SLinus Torvalds 							   file descriptor list for device */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static struct class_interface sg_interface = {
111ee959b00STony Jones 	.add_dev	= sg_add,
112ee959b00STony Jones 	.remove_dev	= sg_remove,
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
1161da177e4SLinus Torvalds 	unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
117ea312552SFUJITA Tomonori 	unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
1181da177e4SLinus Torvalds 	unsigned bufflen;	/* Size of (aggregate) data buffer */
11910db10d1SFUJITA Tomonori 	struct page **pages;
12010db10d1SFUJITA Tomonori 	int page_order;
1211da177e4SLinus Torvalds 	char dio_in_use;	/* 0->indirect IO (or mmap), 1->dio */
1221da177e4SLinus Torvalds 	unsigned char cmd_opcode; /* first byte of command */
1231da177e4SLinus Torvalds } Sg_scatter_hold;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds struct sg_device;		/* forward declarations */
1261da177e4SLinus Torvalds struct sg_fd;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds typedef struct sg_request {	/* SG_MAX_QUEUE requests outstanding per file */
1291da177e4SLinus Torvalds 	struct sg_request *nextrp;	/* NULL -> tail request (slist) */
1301da177e4SLinus Torvalds 	struct sg_fd *parentfp;	/* NULL -> not in use */
1311da177e4SLinus Torvalds 	Sg_scatter_hold data;	/* hold buffer, perhaps scatter list */
1321da177e4SLinus Torvalds 	sg_io_hdr_t header;	/* scsi command+info, see <scsi/sg.h> */
133d6b10348SMike Christie 	unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
1341da177e4SLinus Torvalds 	char res_used;		/* 1 -> using reserve buffer, 0 -> not ... */
1351da177e4SLinus Torvalds 	char orphan;		/* 1 -> drop on sight, 0 -> normal */
1361da177e4SLinus Torvalds 	char sg_io_owned;	/* 1 -> packet belongs to SG_IO */
1371da177e4SLinus Torvalds 	volatile char done;	/* 0->before bh, 1->before read, 2->read */
13810865dfaSFUJITA Tomonori 	struct request *rq;
1396e5a30cbSFUJITA Tomonori 	struct bio *bio;
1401da177e4SLinus Torvalds } Sg_request;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds typedef struct sg_fd {		/* holds the state of a file descriptor */
1431da177e4SLinus Torvalds 	struct sg_fd *nextfp;	/* NULL when last opened fd on this device */
1441da177e4SLinus Torvalds 	struct sg_device *parentdp;	/* owning device */
1451da177e4SLinus Torvalds 	wait_queue_head_t read_wait;	/* queue read until command done */
1461da177e4SLinus Torvalds 	rwlock_t rq_list_lock;	/* protect access to list in req_arr */
1471da177e4SLinus Torvalds 	int timeout;		/* defaults to SG_DEFAULT_TIMEOUT      */
1481da177e4SLinus Torvalds 	int timeout_user;	/* defaults to SG_DEFAULT_TIMEOUT_USER */
1491da177e4SLinus Torvalds 	Sg_scatter_hold reserve;	/* buffer held for this file descriptor */
1501da177e4SLinus Torvalds 	unsigned save_scat_len;	/* original length of trunc. scat. element */
1511da177e4SLinus Torvalds 	Sg_request *headrp;	/* head of request slist, NULL->empty */
1521da177e4SLinus Torvalds 	struct fasync_struct *async_qp;	/* used by asynchronous notification */
1531da177e4SLinus Torvalds 	Sg_request req_arr[SG_MAX_QUEUE];	/* used as singly-linked list */
1541da177e4SLinus Torvalds 	char low_dma;		/* as in parent but possibly overridden to 1 */
1551da177e4SLinus Torvalds 	char force_packid;	/* 1 -> pack_id input to read(), 0 -> ignored */
1561da177e4SLinus Torvalds 	volatile char closed;	/* 1 -> fd closed but request(s) outstanding */
1571da177e4SLinus Torvalds 	char cmd_q;		/* 1 -> allow command queuing, 0 -> don't */
1581da177e4SLinus Torvalds 	char next_cmd_len;	/* 0 -> automatic (def), >0 -> use on next write() */
1591da177e4SLinus Torvalds 	char keep_orphan;	/* 0 -> drop orphan (def), 1 -> keep for read() */
1601da177e4SLinus Torvalds 	char mmap_called;	/* 0 -> mmap() never called on this fd */
1611da177e4SLinus Torvalds } Sg_fd;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds typedef struct sg_device { /* holds the state of each scsi generic device */
1641da177e4SLinus Torvalds 	struct scsi_device *device;
1651da177e4SLinus Torvalds 	wait_queue_head_t o_excl_wait;	/* queue open() when O_EXCL in use */
1661da177e4SLinus Torvalds 	int sg_tablesize;	/* adapter's max scatter-gather table size */
1677c07d613SJames Bottomley 	u32 index;		/* device index number */
1681da177e4SLinus Torvalds 	Sg_fd *headfp;		/* first open fd belonging to this device */
1691da177e4SLinus Torvalds 	volatile char detached;	/* 0->attached, 1->detached pending removal */
1701da177e4SLinus Torvalds 	volatile char exclude;	/* opened for exclusive access */
1711da177e4SLinus Torvalds 	char sgdebug;		/* 0->off, 1->sense, 9->dump dev, 10-> all devs */
1721da177e4SLinus Torvalds 	struct gendisk *disk;
1731da177e4SLinus Torvalds 	struct cdev * cdev;	/* char_dev [sysfs: /sys/cdev/major/sg<n>] */
1741da177e4SLinus Torvalds } Sg_device;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static int sg_fasync(int fd, struct file *filp, int mode);
177d6b10348SMike Christie /* tasklet or soft irq callback */
178a91a3a20SFUJITA Tomonori static void sg_rq_end_io(struct request *rq, int uptodate);
17910865dfaSFUJITA Tomonori static int sg_start_req(Sg_request *srp, unsigned char *cmd);
1801da177e4SLinus Torvalds static void sg_finish_rem_req(Sg_request * srp);
1811da177e4SLinus Torvalds static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
1821da177e4SLinus Torvalds static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
1831da177e4SLinus Torvalds 			 int tablesize);
1841da177e4SLinus Torvalds static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
1851da177e4SLinus Torvalds 			   Sg_request * srp);
1860b07de85SAdel Gadllah static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
1870b07de85SAdel Gadllah 			const char __user *buf, size_t count, int blocking,
1880b07de85SAdel Gadllah 			int read_only, Sg_request **o_srp);
1891da177e4SLinus Torvalds static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
1901da177e4SLinus Torvalds 			   unsigned char *cmnd, int timeout, int blocking);
1911da177e4SLinus Torvalds static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
1921da177e4SLinus Torvalds static void sg_remove_scat(Sg_scatter_hold * schp);
1931da177e4SLinus Torvalds static void sg_build_reserve(Sg_fd * sfp, int req_size);
1941da177e4SLinus Torvalds static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
1951da177e4SLinus Torvalds static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
1961da177e4SLinus Torvalds static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
1971da177e4SLinus Torvalds static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
1981da177e4SLinus Torvalds static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
1991da177e4SLinus Torvalds static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
2001da177e4SLinus Torvalds static Sg_request *sg_add_request(Sg_fd * sfp);
2011da177e4SLinus Torvalds static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
2021da177e4SLinus Torvalds static int sg_res_in_use(Sg_fd * sfp);
2031da177e4SLinus Torvalds static Sg_device *sg_get_dev(int dev);
2041da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
2051da177e4SLinus Torvalds static int sg_last_dev(void);
2061da177e4SLinus Torvalds #endif
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #define SZ_SG_HEADER sizeof(struct sg_header)
2091da177e4SLinus Torvalds #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
2101da177e4SLinus Torvalds #define SZ_SG_IOVEC sizeof(sg_iovec_t)
2111da177e4SLinus Torvalds #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
2121da177e4SLinus Torvalds 
21314e507b8SFUJITA Tomonori static int sg_allow_access(struct file *filp, unsigned char *cmd)
21414e507b8SFUJITA Tomonori {
21514e507b8SFUJITA Tomonori 	struct sg_fd *sfp = (struct sg_fd *)filp->private_data;
21614e507b8SFUJITA Tomonori 	struct request_queue *q = sfp->parentdp->device->request_queue;
21714e507b8SFUJITA Tomonori 
21814e507b8SFUJITA Tomonori 	if (sfp->parentdp->device->type == TYPE_SCANNER)
21914e507b8SFUJITA Tomonori 		return 0;
22014e507b8SFUJITA Tomonori 
22114e507b8SFUJITA Tomonori 	return blk_verify_command(&q->cmd_filter,
22214e507b8SFUJITA Tomonori 				  cmd, filp->f_mode & FMODE_WRITE);
22314e507b8SFUJITA Tomonori }
22414e507b8SFUJITA Tomonori 
2251da177e4SLinus Torvalds static int
2261da177e4SLinus Torvalds sg_open(struct inode *inode, struct file *filp)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	int dev = iminor(inode);
2291da177e4SLinus Torvalds 	int flags = filp->f_flags;
230d6b10348SMike Christie 	struct request_queue *q;
2311da177e4SLinus Torvalds 	Sg_device *sdp;
2321da177e4SLinus Torvalds 	Sg_fd *sfp;
2331da177e4SLinus Torvalds 	int res;
2341da177e4SLinus Torvalds 	int retval;
2351da177e4SLinus Torvalds 
236eb09d3d4SJonathan Corbet 	lock_kernel();
2371da177e4SLinus Torvalds 	nonseekable_open(inode, filp);
2381da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
2391da177e4SLinus Torvalds 	sdp = sg_get_dev(dev);
240eb09d3d4SJonathan Corbet 	if ((!sdp) || (!sdp->device)) {
241eb09d3d4SJonathan Corbet 		unlock_kernel();
2421da177e4SLinus Torvalds 		return -ENXIO;
243eb09d3d4SJonathan Corbet 	}
244eb09d3d4SJonathan Corbet 	if (sdp->detached) {
245eb09d3d4SJonathan Corbet 		unlock_kernel();
2461da177e4SLinus Torvalds 		return -ENODEV;
247eb09d3d4SJonathan Corbet 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/* This driver's module count bumped by fops_get in <linux/fs.h> */
2501da177e4SLinus Torvalds 	/* Prevent the device driver from vanishing while we sleep */
2511da177e4SLinus Torvalds 	retval = scsi_device_get(sdp->device);
252eb09d3d4SJonathan Corbet 	if (retval) {
253eb09d3d4SJonathan Corbet 		unlock_kernel();
2541da177e4SLinus Torvalds 		return retval;
255eb09d3d4SJonathan Corbet 	}
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (!((flags & O_NONBLOCK) ||
2581da177e4SLinus Torvalds 	      scsi_block_when_processing_errors(sdp->device))) {
2591da177e4SLinus Torvalds 		retval = -ENXIO;
2601da177e4SLinus Torvalds 		/* we are in error recovery for this device */
2611da177e4SLinus Torvalds 		goto error_out;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (flags & O_EXCL) {
2651da177e4SLinus Torvalds 		if (O_RDONLY == (flags & O_ACCMODE)) {
2661da177e4SLinus Torvalds 			retval = -EPERM; /* Can't lock it with read only access */
2671da177e4SLinus Torvalds 			goto error_out;
2681da177e4SLinus Torvalds 		}
2691da177e4SLinus Torvalds 		if (sdp->headfp && (flags & O_NONBLOCK)) {
2701da177e4SLinus Torvalds 			retval = -EBUSY;
2711da177e4SLinus Torvalds 			goto error_out;
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 		res = 0;
2741da177e4SLinus Torvalds 		__wait_event_interruptible(sdp->o_excl_wait,
2751da177e4SLinus Torvalds 			((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
2761da177e4SLinus Torvalds 		if (res) {
2771da177e4SLinus Torvalds 			retval = res;	/* -ERESTARTSYS because signal hit process */
2781da177e4SLinus Torvalds 			goto error_out;
2791da177e4SLinus Torvalds 		}
2801da177e4SLinus Torvalds 	} else if (sdp->exclude) {	/* some other fd has an exclusive lock on dev */
2811da177e4SLinus Torvalds 		if (flags & O_NONBLOCK) {
2821da177e4SLinus Torvalds 			retval = -EBUSY;
2831da177e4SLinus Torvalds 			goto error_out;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 		res = 0;
2861da177e4SLinus Torvalds 		__wait_event_interruptible(sdp->o_excl_wait, (!sdp->exclude),
2871da177e4SLinus Torvalds 					   res);
2881da177e4SLinus Torvalds 		if (res) {
2891da177e4SLinus Torvalds 			retval = res;	/* -ERESTARTSYS because signal hit process */
2901da177e4SLinus Torvalds 			goto error_out;
2911da177e4SLinus Torvalds 		}
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	if (sdp->detached) {
2941da177e4SLinus Torvalds 		retval = -ENODEV;
2951da177e4SLinus Torvalds 		goto error_out;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	if (!sdp->headfp) {	/* no existing opens on this device */
2981da177e4SLinus Torvalds 		sdp->sgdebug = 0;
299d6b10348SMike Christie 		q = sdp->device->request_queue;
300d6b10348SMike Christie 		sdp->sg_tablesize = min(q->max_hw_segments,
301d6b10348SMike Christie 					q->max_phys_segments);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	if ((sfp = sg_add_sfp(sdp, dev)))
3041da177e4SLinus Torvalds 		filp->private_data = sfp;
3051da177e4SLinus Torvalds 	else {
3061da177e4SLinus Torvalds 		if (flags & O_EXCL)
3071da177e4SLinus Torvalds 			sdp->exclude = 0;	/* undo if error */
3081da177e4SLinus Torvalds 		retval = -ENOMEM;
3091da177e4SLinus Torvalds 		goto error_out;
3101da177e4SLinus Torvalds 	}
311eb09d3d4SJonathan Corbet 	unlock_kernel();
3121da177e4SLinus Torvalds 	return 0;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds       error_out:
3151da177e4SLinus Torvalds 	scsi_device_put(sdp->device);
316eb09d3d4SJonathan Corbet 	unlock_kernel();
3171da177e4SLinus Torvalds 	return retval;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /* Following function was formerly called 'sg_close' */
3211da177e4SLinus Torvalds static int
3221da177e4SLinus Torvalds sg_release(struct inode *inode, struct file *filp)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	Sg_device *sdp;
3251da177e4SLinus Torvalds 	Sg_fd *sfp;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
3281da177e4SLinus Torvalds 		return -ENXIO;
3291da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
3301da177e4SLinus Torvalds 	sg_fasync(-1, filp, 0);	/* remove filp from async notification list */
3311da177e4SLinus Torvalds 	if (0 == sg_remove_sfp(sdp, sfp)) {	/* Returns 1 when sdp gone */
3321da177e4SLinus Torvalds 		if (!sdp->detached) {
3331da177e4SLinus Torvalds 			scsi_device_put(sdp->device);
3341da177e4SLinus Torvalds 		}
3351da177e4SLinus Torvalds 		sdp->exclude = 0;
3361da177e4SLinus Torvalds 		wake_up_interruptible(&sdp->o_excl_wait);
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	return 0;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static ssize_t
3421da177e4SLinus Torvalds sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	Sg_device *sdp;
3451da177e4SLinus Torvalds 	Sg_fd *sfp;
3461da177e4SLinus Torvalds 	Sg_request *srp;
3471da177e4SLinus Torvalds 	int req_pack_id = -1;
3481da177e4SLinus Torvalds 	sg_io_hdr_t *hp;
349cb59e840S 	struct sg_header *old_hdr = NULL;
350cb59e840S 	int retval = 0;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
3531da177e4SLinus Torvalds 		return -ENXIO;
3541da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
3551da177e4SLinus Torvalds 				   sdp->disk->disk_name, (int) count));
356d6b10348SMike Christie 
3571da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, buf, count))
3581da177e4SLinus Torvalds 		return -EFAULT;
3591da177e4SLinus Torvalds 	if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
360cb59e840S 		old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
361cb59e840S 		if (!old_hdr)
362cb59e840S 			return -ENOMEM;
363cb59e840S 		if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
364cb59e840S 			retval = -EFAULT;
365cb59e840S 			goto free_old_hdr;
366cb59e840S 		}
367cb59e840S 		if (old_hdr->reply_len < 0) {
3681da177e4SLinus Torvalds 			if (count >= SZ_SG_IO_HDR) {
369cb59e840S 				sg_io_hdr_t *new_hdr;
370cb59e840S 				new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
371cb59e840S 				if (!new_hdr) {
372cb59e840S 					retval = -ENOMEM;
373cb59e840S 					goto free_old_hdr;
374cb59e840S 				}
375cb59e840S 				retval =__copy_from_user
376cb59e840S 				    (new_hdr, buf, SZ_SG_IO_HDR);
377cb59e840S 				req_pack_id = new_hdr->pack_id;
378cb59e840S 				kfree(new_hdr);
379cb59e840S 				if (retval) {
380cb59e840S 					retval = -EFAULT;
381cb59e840S 					goto free_old_hdr;
382cb59e840S 				}
3831da177e4SLinus Torvalds 			}
3841da177e4SLinus Torvalds 		} else
385cb59e840S 			req_pack_id = old_hdr->pack_id;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	srp = sg_get_rq_mark(sfp, req_pack_id);
3881da177e4SLinus Torvalds 	if (!srp) {		/* now wait on packet to arrive */
389cb59e840S 		if (sdp->detached) {
390cb59e840S 			retval = -ENODEV;
391cb59e840S 			goto free_old_hdr;
392cb59e840S 		}
393cb59e840S 		if (filp->f_flags & O_NONBLOCK) {
394cb59e840S 			retval = -EAGAIN;
395cb59e840S 			goto free_old_hdr;
396cb59e840S 		}
3971da177e4SLinus Torvalds 		while (1) {
398cb59e840S 			retval = 0; /* following macro beats race condition */
3991da177e4SLinus Torvalds 			__wait_event_interruptible(sfp->read_wait,
400cb59e840S 				(sdp->detached ||
401cb59e840S 				(srp = sg_get_rq_mark(sfp, req_pack_id))),
402cb59e840S 				retval);
403cb59e840S 			if (sdp->detached) {
404cb59e840S 				retval = -ENODEV;
405cb59e840S 				goto free_old_hdr;
406cb59e840S 			}
407cb59e840S 			if (0 == retval)
4081da177e4SLinus Torvalds 				break;
409cb59e840S 
410cb59e840S 			/* -ERESTARTSYS as signal hit process */
411cb59e840S 			goto free_old_hdr;
4121da177e4SLinus Torvalds 		}
4131da177e4SLinus Torvalds 	}
414cb59e840S 	if (srp->header.interface_id != '\0') {
415cb59e840S 		retval = sg_new_read(sfp, buf, count, srp);
416cb59e840S 		goto free_old_hdr;
417cb59e840S 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	hp = &srp->header;
420cb59e840S 	if (old_hdr == NULL) {
421cb59e840S 		old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
422cb59e840S 		if (! old_hdr) {
423cb59e840S 			retval = -ENOMEM;
424cb59e840S 			goto free_old_hdr;
425cb59e840S 		}
426cb59e840S 	}
427cb59e840S 	memset(old_hdr, 0, SZ_SG_HEADER);
428cb59e840S 	old_hdr->reply_len = (int) hp->timeout;
429cb59e840S 	old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
430cb59e840S 	old_hdr->pack_id = hp->pack_id;
431cb59e840S 	old_hdr->twelve_byte =
4321da177e4SLinus Torvalds 	    ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
433cb59e840S 	old_hdr->target_status = hp->masked_status;
434cb59e840S 	old_hdr->host_status = hp->host_status;
435cb59e840S 	old_hdr->driver_status = hp->driver_status;
4361da177e4SLinus Torvalds 	if ((CHECK_CONDITION & hp->masked_status) ||
4371da177e4SLinus Torvalds 	    (DRIVER_SENSE & hp->driver_status))
438cb59e840S 		memcpy(old_hdr->sense_buffer, srp->sense_b,
439cb59e840S 		       sizeof (old_hdr->sense_buffer));
4401da177e4SLinus Torvalds 	switch (hp->host_status) {
4411da177e4SLinus Torvalds 	/* This setup of 'result' is for backward compatibility and is best
4421da177e4SLinus Torvalds 	   ignored by the user who should use target, host + driver status */
4431da177e4SLinus Torvalds 	case DID_OK:
4441da177e4SLinus Torvalds 	case DID_PASSTHROUGH:
4451da177e4SLinus Torvalds 	case DID_SOFT_ERROR:
446cb59e840S 		old_hdr->result = 0;
4471da177e4SLinus Torvalds 		break;
4481da177e4SLinus Torvalds 	case DID_NO_CONNECT:
4491da177e4SLinus Torvalds 	case DID_BUS_BUSY:
4501da177e4SLinus Torvalds 	case DID_TIME_OUT:
451cb59e840S 		old_hdr->result = EBUSY;
4521da177e4SLinus Torvalds 		break;
4531da177e4SLinus Torvalds 	case DID_BAD_TARGET:
4541da177e4SLinus Torvalds 	case DID_ABORT:
4551da177e4SLinus Torvalds 	case DID_PARITY:
4561da177e4SLinus Torvalds 	case DID_RESET:
4571da177e4SLinus Torvalds 	case DID_BAD_INTR:
458cb59e840S 		old_hdr->result = EIO;
4591da177e4SLinus Torvalds 		break;
4601da177e4SLinus Torvalds 	case DID_ERROR:
461cb59e840S 		old_hdr->result = (srp->sense_b[0] == 0 &&
4621da177e4SLinus Torvalds 				  hp->masked_status == GOOD) ? 0 : EIO;
4631da177e4SLinus Torvalds 		break;
4641da177e4SLinus Torvalds 	default:
465cb59e840S 		old_hdr->result = EIO;
4661da177e4SLinus Torvalds 		break;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* Now copy the result back to the user buffer.  */
4701da177e4SLinus Torvalds 	if (count >= SZ_SG_HEADER) {
471cb59e840S 		if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
472cb59e840S 			retval = -EFAULT;
473cb59e840S 			goto free_old_hdr;
474cb59e840S 		}
4751da177e4SLinus Torvalds 		buf += SZ_SG_HEADER;
476cb59e840S 		if (count > old_hdr->reply_len)
477cb59e840S 			count = old_hdr->reply_len;
4781da177e4SLinus Torvalds 		if (count > SZ_SG_HEADER) {
479cb59e840S 			if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
480cb59e840S 				retval = -EFAULT;
481cb59e840S 				goto free_old_hdr;
482cb59e840S 			}
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	} else
485cb59e840S 		count = (old_hdr->result == 0) ? 0 : -EIO;
4861da177e4SLinus Torvalds 	sg_finish_rem_req(srp);
487cb59e840S 	retval = count;
488cb59e840S free_old_hdr:
489cb59e840S 	kfree(old_hdr);
490cb59e840S 	return retval;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static ssize_t
4941da177e4SLinus Torvalds sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	sg_io_hdr_t *hp = &srp->header;
4971da177e4SLinus Torvalds 	int err = 0;
4981da177e4SLinus Torvalds 	int len;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	if (count < SZ_SG_IO_HDR) {
5011da177e4SLinus Torvalds 		err = -EINVAL;
5021da177e4SLinus Torvalds 		goto err_out;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	hp->sb_len_wr = 0;
5051da177e4SLinus Torvalds 	if ((hp->mx_sb_len > 0) && hp->sbp) {
5061da177e4SLinus Torvalds 		if ((CHECK_CONDITION & hp->masked_status) ||
5071da177e4SLinus Torvalds 		    (DRIVER_SENSE & hp->driver_status)) {
508d6b10348SMike Christie 			int sb_len = SCSI_SENSE_BUFFERSIZE;
5091da177e4SLinus Torvalds 			sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
5101da177e4SLinus Torvalds 			len = 8 + (int) srp->sense_b[7];	/* Additional sense length field */
5111da177e4SLinus Torvalds 			len = (len > sb_len) ? sb_len : len;
5121da177e4SLinus Torvalds 			if (copy_to_user(hp->sbp, srp->sense_b, len)) {
5131da177e4SLinus Torvalds 				err = -EFAULT;
5141da177e4SLinus Torvalds 				goto err_out;
5151da177e4SLinus Torvalds 			}
5161da177e4SLinus Torvalds 			hp->sb_len_wr = len;
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	if (hp->masked_status || hp->host_status || hp->driver_status)
5201da177e4SLinus Torvalds 		hp->info |= SG_INFO_CHECK;
5211da177e4SLinus Torvalds 	if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
5221da177e4SLinus Torvalds 		err = -EFAULT;
5231da177e4SLinus Torvalds 		goto err_out;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds err_out:
5261da177e4SLinus Torvalds 	sg_finish_rem_req(srp);
5271da177e4SLinus Torvalds 	return (0 == err) ? count : err;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds static ssize_t
5311da177e4SLinus Torvalds sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	int mxsize, cmd_size, k;
5341da177e4SLinus Torvalds 	int input_size, blocking;
5351da177e4SLinus Torvalds 	unsigned char opcode;
5361da177e4SLinus Torvalds 	Sg_device *sdp;
5371da177e4SLinus Torvalds 	Sg_fd *sfp;
5381da177e4SLinus Torvalds 	Sg_request *srp;
5391da177e4SLinus Torvalds 	struct sg_header old_hdr;
5401da177e4SLinus Torvalds 	sg_io_hdr_t *hp;
541d6b10348SMike Christie 	unsigned char cmnd[MAX_COMMAND_SIZE];
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
5441da177e4SLinus Torvalds 		return -ENXIO;
5451da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_write: %s, count=%d\n",
5461da177e4SLinus Torvalds 				   sdp->disk->disk_name, (int) count));
5471da177e4SLinus Torvalds 	if (sdp->detached)
5481da177e4SLinus Torvalds 		return -ENODEV;
5491da177e4SLinus Torvalds 	if (!((filp->f_flags & O_NONBLOCK) ||
5501da177e4SLinus Torvalds 	      scsi_block_when_processing_errors(sdp->device)))
5511da177e4SLinus Torvalds 		return -ENXIO;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, buf, count))
5541da177e4SLinus Torvalds 		return -EFAULT;	/* protects following copy_from_user()s + get_user()s */
5551da177e4SLinus Torvalds 	if (count < SZ_SG_HEADER)
5561da177e4SLinus Torvalds 		return -EIO;
5571da177e4SLinus Torvalds 	if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
5581da177e4SLinus Torvalds 		return -EFAULT;
5591da177e4SLinus Torvalds 	blocking = !(filp->f_flags & O_NONBLOCK);
5601da177e4SLinus Torvalds 	if (old_hdr.reply_len < 0)
5610b07de85SAdel Gadllah 		return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
5621da177e4SLinus Torvalds 	if (count < (SZ_SG_HEADER + 6))
5631da177e4SLinus Torvalds 		return -EIO;	/* The minimum scsi command length is 6 bytes. */
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (!(srp = sg_add_request(sfp))) {
5661da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(1, printk("sg_write: queue full\n"));
5671da177e4SLinus Torvalds 		return -EDOM;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	buf += SZ_SG_HEADER;
5701da177e4SLinus Torvalds 	__get_user(opcode, buf);
5711da177e4SLinus Torvalds 	if (sfp->next_cmd_len > 0) {
5721da177e4SLinus Torvalds 		if (sfp->next_cmd_len > MAX_COMMAND_SIZE) {
5731da177e4SLinus Torvalds 			SCSI_LOG_TIMEOUT(1, printk("sg_write: command length too long\n"));
5741da177e4SLinus Torvalds 			sfp->next_cmd_len = 0;
5751da177e4SLinus Torvalds 			sg_remove_request(sfp, srp);
5761da177e4SLinus Torvalds 			return -EIO;
5771da177e4SLinus Torvalds 		}
5781da177e4SLinus Torvalds 		cmd_size = sfp->next_cmd_len;
5791da177e4SLinus Torvalds 		sfp->next_cmd_len = 0;	/* reset so only this write() effected */
5801da177e4SLinus Torvalds 	} else {
5811da177e4SLinus Torvalds 		cmd_size = COMMAND_SIZE(opcode);	/* based on SCSI command group */
5821da177e4SLinus Torvalds 		if ((opcode >= 0xc0) && old_hdr.twelve_byte)
5831da177e4SLinus Torvalds 			cmd_size = 12;
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk(
5861da177e4SLinus Torvalds 		"sg_write:   scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
5871da177e4SLinus Torvalds /* Determine buffer size.  */
5881da177e4SLinus Torvalds 	input_size = count - cmd_size;
5891da177e4SLinus Torvalds 	mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
5901da177e4SLinus Torvalds 	mxsize -= SZ_SG_HEADER;
5911da177e4SLinus Torvalds 	input_size -= SZ_SG_HEADER;
5921da177e4SLinus Torvalds 	if (input_size < 0) {
5931da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
5941da177e4SLinus Torvalds 		return -EIO;	/* User did not pass enough bytes for this command. */
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	hp = &srp->header;
5971da177e4SLinus Torvalds 	hp->interface_id = '\0';	/* indicator of old interface tunnelled */
5981da177e4SLinus Torvalds 	hp->cmd_len = (unsigned char) cmd_size;
5991da177e4SLinus Torvalds 	hp->iovec_count = 0;
6001da177e4SLinus Torvalds 	hp->mx_sb_len = 0;
6011da177e4SLinus Torvalds 	if (input_size > 0)
6021da177e4SLinus Torvalds 		hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
6031da177e4SLinus Torvalds 		    SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
6041da177e4SLinus Torvalds 	else
6051da177e4SLinus Torvalds 		hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
6061da177e4SLinus Torvalds 	hp->dxfer_len = mxsize;
607fad7f01eSFUJITA Tomonori 	if (hp->dxfer_direction == SG_DXFER_TO_DEV)
6081da177e4SLinus Torvalds 		hp->dxferp = (char __user *)buf + cmd_size;
609fad7f01eSFUJITA Tomonori 	else
610fad7f01eSFUJITA Tomonori 		hp->dxferp = NULL;
6111da177e4SLinus Torvalds 	hp->sbp = NULL;
6121da177e4SLinus Torvalds 	hp->timeout = old_hdr.reply_len;	/* structure abuse ... */
6131da177e4SLinus Torvalds 	hp->flags = input_size;	/* structure abuse ... */
6141da177e4SLinus Torvalds 	hp->pack_id = old_hdr.pack_id;
6151da177e4SLinus Torvalds 	hp->usr_ptr = NULL;
6161da177e4SLinus Torvalds 	if (__copy_from_user(cmnd, buf, cmd_size))
6171da177e4SLinus Torvalds 		return -EFAULT;
6181da177e4SLinus Torvalds 	/*
6191da177e4SLinus Torvalds 	 * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
6201da177e4SLinus Torvalds 	 * but is is possible that the app intended SG_DXFER_TO_DEV, because there
6211da177e4SLinus Torvalds 	 * is a non-zero input_size, so emit a warning.
6221da177e4SLinus Torvalds 	 */
623eaa3e22eSAndi Kleen 	if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
624eaa3e22eSAndi Kleen 		static char cmd[TASK_COMM_LEN];
625eaa3e22eSAndi Kleen 		if (strcmp(current->comm, cmd) && printk_ratelimit()) {
6261da177e4SLinus Torvalds 			printk(KERN_WARNING
6271da177e4SLinus Torvalds 			       "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
6281da177e4SLinus Torvalds 			       "guessing data in;\n" KERN_WARNING "   "
6291da177e4SLinus Torvalds 			       "program %s not setting count and/or reply_len properly\n",
6301da177e4SLinus Torvalds 			       old_hdr.reply_len - (int)SZ_SG_HEADER,
6311da177e4SLinus Torvalds 			       input_size, (unsigned int) cmnd[0],
6321da177e4SLinus Torvalds 			       current->comm);
633eaa3e22eSAndi Kleen 			strcpy(cmd, current->comm);
634eaa3e22eSAndi Kleen 		}
635eaa3e22eSAndi Kleen 	}
6361da177e4SLinus Torvalds 	k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
6371da177e4SLinus Torvalds 	return (k < 0) ? k : count;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static ssize_t
6410b07de85SAdel Gadllah sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
6420b07de85SAdel Gadllah 		 size_t count, int blocking, int read_only,
6430b07de85SAdel Gadllah 		 Sg_request **o_srp)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	int k;
6461da177e4SLinus Torvalds 	Sg_request *srp;
6471da177e4SLinus Torvalds 	sg_io_hdr_t *hp;
648d6b10348SMike Christie 	unsigned char cmnd[MAX_COMMAND_SIZE];
6491da177e4SLinus Torvalds 	int timeout;
6501da177e4SLinus Torvalds 	unsigned long ul_timeout;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (count < SZ_SG_IO_HDR)
6531da177e4SLinus Torvalds 		return -EINVAL;
6541da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, buf, count))
6551da177e4SLinus Torvalds 		return -EFAULT; /* protects following copy_from_user()s + get_user()s */
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	sfp->cmd_q = 1;	/* when sg_io_hdr seen, set command queuing on */
6581da177e4SLinus Torvalds 	if (!(srp = sg_add_request(sfp))) {
6591da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
6601da177e4SLinus Torvalds 		return -EDOM;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 	hp = &srp->header;
6631da177e4SLinus Torvalds 	if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
6641da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
6651da177e4SLinus Torvalds 		return -EFAULT;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	if (hp->interface_id != 'S') {
6681da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
6691da177e4SLinus Torvalds 		return -ENOSYS;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	if (hp->flags & SG_FLAG_MMAP_IO) {
6721da177e4SLinus Torvalds 		if (hp->dxfer_len > sfp->reserve.bufflen) {
6731da177e4SLinus Torvalds 			sg_remove_request(sfp, srp);
6741da177e4SLinus Torvalds 			return -ENOMEM;	/* MMAP_IO size must fit in reserve buffer */
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 		if (hp->flags & SG_FLAG_DIRECT_IO) {
6771da177e4SLinus Torvalds 			sg_remove_request(sfp, srp);
6781da177e4SLinus Torvalds 			return -EINVAL;	/* either MMAP_IO or DIRECT_IO (not both) */
6791da177e4SLinus Torvalds 		}
6801da177e4SLinus Torvalds 		if (sg_res_in_use(sfp)) {
6811da177e4SLinus Torvalds 			sg_remove_request(sfp, srp);
6821da177e4SLinus Torvalds 			return -EBUSY;	/* reserve buffer already being used */
6831da177e4SLinus Torvalds 		}
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 	ul_timeout = msecs_to_jiffies(srp->header.timeout);
6861da177e4SLinus Torvalds 	timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
6871da177e4SLinus Torvalds 	if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
6881da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
6891da177e4SLinus Torvalds 		return -EMSGSIZE;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
6921da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
6931da177e4SLinus Torvalds 		return -EFAULT;	/* protects following copy_from_user()s + get_user()s */
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds 	if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
6961da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
6971da177e4SLinus Torvalds 		return -EFAULT;
6981da177e4SLinus Torvalds 	}
69914e507b8SFUJITA Tomonori 	if (read_only && sg_allow_access(file, cmnd)) {
7001da177e4SLinus Torvalds 		sg_remove_request(sfp, srp);
7011da177e4SLinus Torvalds 		return -EPERM;
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 	k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
7041da177e4SLinus Torvalds 	if (k < 0)
7051da177e4SLinus Torvalds 		return k;
7061da177e4SLinus Torvalds 	if (o_srp)
7071da177e4SLinus Torvalds 		*o_srp = srp;
7081da177e4SLinus Torvalds 	return count;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds static int
7121da177e4SLinus Torvalds sg_common_write(Sg_fd * sfp, Sg_request * srp,
7131da177e4SLinus Torvalds 		unsigned char *cmnd, int timeout, int blocking)
7141da177e4SLinus Torvalds {
715d6b10348SMike Christie 	int k, data_dir;
7161da177e4SLinus Torvalds 	Sg_device *sdp = sfp->parentdp;
7171da177e4SLinus Torvalds 	sg_io_hdr_t *hp = &srp->header;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	srp->data.cmd_opcode = cmnd[0];	/* hold opcode of command */
7201da177e4SLinus Torvalds 	hp->status = 0;
7211da177e4SLinus Torvalds 	hp->masked_status = 0;
7221da177e4SLinus Torvalds 	hp->msg_status = 0;
7231da177e4SLinus Torvalds 	hp->info = 0;
7241da177e4SLinus Torvalds 	hp->host_status = 0;
7251da177e4SLinus Torvalds 	hp->driver_status = 0;
7261da177e4SLinus Torvalds 	hp->resid = 0;
7271da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_common_write:  scsi opcode=0x%02x, cmd_size=%d\n",
7281da177e4SLinus Torvalds 			  (int) cmnd[0], (int) hp->cmd_len));
7291da177e4SLinus Torvalds 
73010865dfaSFUJITA Tomonori 	k = sg_start_req(srp, cmnd);
73110865dfaSFUJITA Tomonori 	if (k) {
7327ca63cb4SDouglas Gilbert 		SCSI_LOG_TIMEOUT(1, printk("sg_common_write: start_req err=%d\n", k));
7331da177e4SLinus Torvalds 		sg_finish_rem_req(srp);
7341da177e4SLinus Torvalds 		return k;	/* probably out of space --> ENOMEM */
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 	if (sdp->detached) {
7371da177e4SLinus Torvalds 		sg_finish_rem_req(srp);
7381da177e4SLinus Torvalds 		return -ENODEV;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	switch (hp->dxfer_direction) {
7421da177e4SLinus Torvalds 	case SG_DXFER_TO_FROM_DEV:
7431da177e4SLinus Torvalds 	case SG_DXFER_FROM_DEV:
744d6b10348SMike Christie 		data_dir = DMA_FROM_DEVICE;
7451da177e4SLinus Torvalds 		break;
7461da177e4SLinus Torvalds 	case SG_DXFER_TO_DEV:
747d6b10348SMike Christie 		data_dir = DMA_TO_DEVICE;
7481da177e4SLinus Torvalds 		break;
7491da177e4SLinus Torvalds 	case SG_DXFER_UNKNOWN:
750d6b10348SMike Christie 		data_dir = DMA_BIDIRECTIONAL;
7511da177e4SLinus Torvalds 		break;
7521da177e4SLinus Torvalds 	default:
753d6b10348SMike Christie 		data_dir = DMA_NONE;
7541da177e4SLinus Torvalds 		break;
7551da177e4SLinus Torvalds 	}
756cb59e840S 	hp->duration = jiffies_to_msecs(jiffies);
75710db10d1SFUJITA Tomonori 
75810865dfaSFUJITA Tomonori 	srp->rq->timeout = timeout;
75910865dfaSFUJITA Tomonori 	blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
76010865dfaSFUJITA Tomonori 			      srp->rq, 1, sg_rq_end_io);
76110865dfaSFUJITA Tomonori 	return 0;
76210865dfaSFUJITA Tomonori }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds static int
7651da177e4SLinus Torvalds sg_srp_done(Sg_request *srp, Sg_fd *sfp)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	unsigned long iflags;
7681da177e4SLinus Torvalds 	int done;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	read_lock_irqsave(&sfp->rq_list_lock, iflags);
7711da177e4SLinus Torvalds 	done = srp->done;
7721da177e4SLinus Torvalds 	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
7731da177e4SLinus Torvalds 	return done;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds static int
7771da177e4SLinus Torvalds sg_ioctl(struct inode *inode, struct file *filp,
7781da177e4SLinus Torvalds 	 unsigned int cmd_in, unsigned long arg)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	void __user *p = (void __user *)arg;
7811da177e4SLinus Torvalds 	int __user *ip = p;
7821da177e4SLinus Torvalds 	int result, val, read_only;
7831da177e4SLinus Torvalds 	Sg_device *sdp;
7841da177e4SLinus Torvalds 	Sg_fd *sfp;
7851da177e4SLinus Torvalds 	Sg_request *srp;
7861da177e4SLinus Torvalds 	unsigned long iflags;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
7891da177e4SLinus Torvalds 		return -ENXIO;
790abf54393SFUJITA Tomonori 
7911da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_ioctl: %s, cmd=0x%x\n",
7921da177e4SLinus Torvalds 				   sdp->disk->disk_name, (int) cmd_in));
7931da177e4SLinus Torvalds 	read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	switch (cmd_in) {
7961da177e4SLinus Torvalds 	case SG_IO:
7971da177e4SLinus Torvalds 		{
7981da177e4SLinus Torvalds 			int blocking = 1;	/* ignore O_NONBLOCK flag */
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 			if (sdp->detached)
8011da177e4SLinus Torvalds 				return -ENODEV;
8021da177e4SLinus Torvalds 			if (!scsi_block_when_processing_errors(sdp->device))
8031da177e4SLinus Torvalds 				return -ENXIO;
8041da177e4SLinus Torvalds 			if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
8051da177e4SLinus Torvalds 				return -EFAULT;
8061da177e4SLinus Torvalds 			result =
8070b07de85SAdel Gadllah 			    sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
8081da177e4SLinus Torvalds 					 blocking, read_only, &srp);
8091da177e4SLinus Torvalds 			if (result < 0)
8101da177e4SLinus Torvalds 				return result;
8111da177e4SLinus Torvalds 			srp->sg_io_owned = 1;
8121da177e4SLinus Torvalds 			while (1) {
8131da177e4SLinus Torvalds 				result = 0;	/* following macro to beat race condition */
8141da177e4SLinus Torvalds 				__wait_event_interruptible(sfp->read_wait,
8151da177e4SLinus Torvalds 					(sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
8161da177e4SLinus Torvalds 							   result);
8171da177e4SLinus Torvalds 				if (sdp->detached)
8181da177e4SLinus Torvalds 					return -ENODEV;
8191da177e4SLinus Torvalds 				if (sfp->closed)
8201da177e4SLinus Torvalds 					return 0;	/* request packet dropped already */
8211da177e4SLinus Torvalds 				if (0 == result)
8221da177e4SLinus Torvalds 					break;
8231da177e4SLinus Torvalds 				srp->orphan = 1;
8241da177e4SLinus Torvalds 				return result;	/* -ERESTARTSYS because signal hit process */
8251da177e4SLinus Torvalds 			}
8261da177e4SLinus Torvalds 			write_lock_irqsave(&sfp->rq_list_lock, iflags);
8271da177e4SLinus Torvalds 			srp->done = 2;
8281da177e4SLinus Torvalds 			write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
8291da177e4SLinus Torvalds 			result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
8301da177e4SLinus Torvalds 			return (result < 0) ? result : 0;
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 	case SG_SET_TIMEOUT:
8331da177e4SLinus Torvalds 		result = get_user(val, ip);
8341da177e4SLinus Torvalds 		if (result)
8351da177e4SLinus Torvalds 			return result;
8361da177e4SLinus Torvalds 		if (val < 0)
8371da177e4SLinus Torvalds 			return -EIO;
8381da177e4SLinus Torvalds 		if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
8391da177e4SLinus Torvalds 		    val = MULDIV (INT_MAX, USER_HZ, HZ);
8401da177e4SLinus Torvalds 		sfp->timeout_user = val;
8411da177e4SLinus Torvalds 		sfp->timeout = MULDIV (val, HZ, USER_HZ);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 		return 0;
8441da177e4SLinus Torvalds 	case SG_GET_TIMEOUT:	/* N.B. User receives timeout as return value */
8451da177e4SLinus Torvalds 				/* strange ..., for backward compatibility */
8461da177e4SLinus Torvalds 		return sfp->timeout_user;
8471da177e4SLinus Torvalds 	case SG_SET_FORCE_LOW_DMA:
8481da177e4SLinus Torvalds 		result = get_user(val, ip);
8491da177e4SLinus Torvalds 		if (result)
8501da177e4SLinus Torvalds 			return result;
8511da177e4SLinus Torvalds 		if (val) {
8521da177e4SLinus Torvalds 			sfp->low_dma = 1;
8531da177e4SLinus Torvalds 			if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
8541da177e4SLinus Torvalds 				val = (int) sfp->reserve.bufflen;
8551da177e4SLinus Torvalds 				sg_remove_scat(&sfp->reserve);
8561da177e4SLinus Torvalds 				sg_build_reserve(sfp, val);
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 		} else {
8591da177e4SLinus Torvalds 			if (sdp->detached)
8601da177e4SLinus Torvalds 				return -ENODEV;
8611da177e4SLinus Torvalds 			sfp->low_dma = sdp->device->host->unchecked_isa_dma;
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 		return 0;
8641da177e4SLinus Torvalds 	case SG_GET_LOW_DMA:
8651da177e4SLinus Torvalds 		return put_user((int) sfp->low_dma, ip);
8661da177e4SLinus Torvalds 	case SG_GET_SCSI_ID:
8671da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
8681da177e4SLinus Torvalds 			return -EFAULT;
8691da177e4SLinus Torvalds 		else {
8701da177e4SLinus Torvalds 			sg_scsi_id_t __user *sg_idp = p;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 			if (sdp->detached)
8731da177e4SLinus Torvalds 				return -ENODEV;
8741da177e4SLinus Torvalds 			__put_user((int) sdp->device->host->host_no,
8751da177e4SLinus Torvalds 				   &sg_idp->host_no);
8761da177e4SLinus Torvalds 			__put_user((int) sdp->device->channel,
8771da177e4SLinus Torvalds 				   &sg_idp->channel);
8781da177e4SLinus Torvalds 			__put_user((int) sdp->device->id, &sg_idp->scsi_id);
8791da177e4SLinus Torvalds 			__put_user((int) sdp->device->lun, &sg_idp->lun);
8801da177e4SLinus Torvalds 			__put_user((int) sdp->device->type, &sg_idp->scsi_type);
8811da177e4SLinus Torvalds 			__put_user((short) sdp->device->host->cmd_per_lun,
8821da177e4SLinus Torvalds 				   &sg_idp->h_cmd_per_lun);
8831da177e4SLinus Torvalds 			__put_user((short) sdp->device->queue_depth,
8841da177e4SLinus Torvalds 				   &sg_idp->d_queue_depth);
8851da177e4SLinus Torvalds 			__put_user(0, &sg_idp->unused[0]);
8861da177e4SLinus Torvalds 			__put_user(0, &sg_idp->unused[1]);
8871da177e4SLinus Torvalds 			return 0;
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 	case SG_SET_FORCE_PACK_ID:
8901da177e4SLinus Torvalds 		result = get_user(val, ip);
8911da177e4SLinus Torvalds 		if (result)
8921da177e4SLinus Torvalds 			return result;
8931da177e4SLinus Torvalds 		sfp->force_packid = val ? 1 : 0;
8941da177e4SLinus Torvalds 		return 0;
8951da177e4SLinus Torvalds 	case SG_GET_PACK_ID:
8961da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
8971da177e4SLinus Torvalds 			return -EFAULT;
8981da177e4SLinus Torvalds 		read_lock_irqsave(&sfp->rq_list_lock, iflags);
8991da177e4SLinus Torvalds 		for (srp = sfp->headrp; srp; srp = srp->nextrp) {
9001da177e4SLinus Torvalds 			if ((1 == srp->done) && (!srp->sg_io_owned)) {
9011da177e4SLinus Torvalds 				read_unlock_irqrestore(&sfp->rq_list_lock,
9021da177e4SLinus Torvalds 						       iflags);
9031da177e4SLinus Torvalds 				__put_user(srp->header.pack_id, ip);
9041da177e4SLinus Torvalds 				return 0;
9051da177e4SLinus Torvalds 			}
9061da177e4SLinus Torvalds 		}
9071da177e4SLinus Torvalds 		read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
9081da177e4SLinus Torvalds 		__put_user(-1, ip);
9091da177e4SLinus Torvalds 		return 0;
9101da177e4SLinus Torvalds 	case SG_GET_NUM_WAITING:
9111da177e4SLinus Torvalds 		read_lock_irqsave(&sfp->rq_list_lock, iflags);
9121da177e4SLinus Torvalds 		for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
9131da177e4SLinus Torvalds 			if ((1 == srp->done) && (!srp->sg_io_owned))
9141da177e4SLinus Torvalds 				++val;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 		read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
9171da177e4SLinus Torvalds 		return put_user(val, ip);
9181da177e4SLinus Torvalds 	case SG_GET_SG_TABLESIZE:
9191da177e4SLinus Torvalds 		return put_user(sdp->sg_tablesize, ip);
9201da177e4SLinus Torvalds 	case SG_SET_RESERVED_SIZE:
9211da177e4SLinus Torvalds 		result = get_user(val, ip);
9221da177e4SLinus Torvalds 		if (result)
9231da177e4SLinus Torvalds 			return result;
9241da177e4SLinus Torvalds                 if (val < 0)
9251da177e4SLinus Torvalds                         return -EINVAL;
92644ec9542SAlan Stern 		val = min_t(int, val,
92744ec9542SAlan Stern 				sdp->device->request_queue->max_sectors * 512);
9281da177e4SLinus Torvalds 		if (val != sfp->reserve.bufflen) {
9291da177e4SLinus Torvalds 			if (sg_res_in_use(sfp) || sfp->mmap_called)
9301da177e4SLinus Torvalds 				return -EBUSY;
9311da177e4SLinus Torvalds 			sg_remove_scat(&sfp->reserve);
9321da177e4SLinus Torvalds 			sg_build_reserve(sfp, val);
9331da177e4SLinus Torvalds 		}
9341da177e4SLinus Torvalds 		return 0;
9351da177e4SLinus Torvalds 	case SG_GET_RESERVED_SIZE:
93644ec9542SAlan Stern 		val = min_t(int, sfp->reserve.bufflen,
93744ec9542SAlan Stern 				sdp->device->request_queue->max_sectors * 512);
9381da177e4SLinus Torvalds 		return put_user(val, ip);
9391da177e4SLinus Torvalds 	case SG_SET_COMMAND_Q:
9401da177e4SLinus Torvalds 		result = get_user(val, ip);
9411da177e4SLinus Torvalds 		if (result)
9421da177e4SLinus Torvalds 			return result;
9431da177e4SLinus Torvalds 		sfp->cmd_q = val ? 1 : 0;
9441da177e4SLinus Torvalds 		return 0;
9451da177e4SLinus Torvalds 	case SG_GET_COMMAND_Q:
9461da177e4SLinus Torvalds 		return put_user((int) sfp->cmd_q, ip);
9471da177e4SLinus Torvalds 	case SG_SET_KEEP_ORPHAN:
9481da177e4SLinus Torvalds 		result = get_user(val, ip);
9491da177e4SLinus Torvalds 		if (result)
9501da177e4SLinus Torvalds 			return result;
9511da177e4SLinus Torvalds 		sfp->keep_orphan = val;
9521da177e4SLinus Torvalds 		return 0;
9531da177e4SLinus Torvalds 	case SG_GET_KEEP_ORPHAN:
9541da177e4SLinus Torvalds 		return put_user((int) sfp->keep_orphan, ip);
9551da177e4SLinus Torvalds 	case SG_NEXT_CMD_LEN:
9561da177e4SLinus Torvalds 		result = get_user(val, ip);
9571da177e4SLinus Torvalds 		if (result)
9581da177e4SLinus Torvalds 			return result;
9591da177e4SLinus Torvalds 		sfp->next_cmd_len = (val > 0) ? val : 0;
9601da177e4SLinus Torvalds 		return 0;
9611da177e4SLinus Torvalds 	case SG_GET_VERSION_NUM:
9621da177e4SLinus Torvalds 		return put_user(sg_version_num, ip);
9631da177e4SLinus Torvalds 	case SG_GET_ACCESS_COUNT:
9641da177e4SLinus Torvalds 		/* faked - we don't have a real access count anymore */
9651da177e4SLinus Torvalds 		val = (sdp->device ? 1 : 0);
9661da177e4SLinus Torvalds 		return put_user(val, ip);
9671da177e4SLinus Torvalds 	case SG_GET_REQUEST_TABLE:
9681da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
9691da177e4SLinus Torvalds 			return -EFAULT;
9701da177e4SLinus Torvalds 		else {
971cb59e840S 			sg_req_info_t *rinfo;
972cb59e840S 			unsigned int ms;
973cb59e840S 
974cb59e840S 			rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
975cb59e840S 								GFP_KERNEL);
976cb59e840S 			if (!rinfo)
977cb59e840S 				return -ENOMEM;
9781da177e4SLinus Torvalds 			read_lock_irqsave(&sfp->rq_list_lock, iflags);
9791da177e4SLinus Torvalds 			for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
9801da177e4SLinus Torvalds 			     ++val, srp = srp ? srp->nextrp : srp) {
9811da177e4SLinus Torvalds 				memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
9821da177e4SLinus Torvalds 				if (srp) {
9831da177e4SLinus Torvalds 					rinfo[val].req_state = srp->done + 1;
9841da177e4SLinus Torvalds 					rinfo[val].problem =
9851da177e4SLinus Torvalds 					    srp->header.masked_status &
9861da177e4SLinus Torvalds 					    srp->header.host_status &
9871da177e4SLinus Torvalds 					    srp->header.driver_status;
988cb59e840S 					if (srp->done)
9891da177e4SLinus Torvalds 						rinfo[val].duration =
990cb59e840S 							srp->header.duration;
991cb59e840S 					else {
992cb59e840S 						ms = jiffies_to_msecs(jiffies);
993cb59e840S 						rinfo[val].duration =
994cb59e840S 						    (ms > srp->header.duration) ?
995cb59e840S 						    (ms - srp->header.duration) : 0;
996cb59e840S 					}
9971da177e4SLinus Torvalds 					rinfo[val].orphan = srp->orphan;
998cb59e840S 					rinfo[val].sg_io_owned =
999cb59e840S 							srp->sg_io_owned;
1000cb59e840S 					rinfo[val].pack_id =
1001cb59e840S 							srp->header.pack_id;
1002cb59e840S 					rinfo[val].usr_ptr =
1003cb59e840S 							srp->header.usr_ptr;
10041da177e4SLinus Torvalds 				}
10051da177e4SLinus Torvalds 			}
10061da177e4SLinus Torvalds 			read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1007cb59e840S 			result = __copy_to_user(p, rinfo,
1008cb59e840S 						SZ_SG_REQ_INFO * SG_MAX_QUEUE);
1009cb59e840S 			result = result ? -EFAULT : 0;
1010cb59e840S 			kfree(rinfo);
1011cb59e840S 			return result;
10121da177e4SLinus Torvalds 		}
10131da177e4SLinus Torvalds 	case SG_EMULATED_HOST:
10141da177e4SLinus Torvalds 		if (sdp->detached)
10151da177e4SLinus Torvalds 			return -ENODEV;
10161da177e4SLinus Torvalds 		return put_user(sdp->device->host->hostt->emulated, ip);
10171da177e4SLinus Torvalds 	case SG_SCSI_RESET:
10181da177e4SLinus Torvalds 		if (sdp->detached)
10191da177e4SLinus Torvalds 			return -ENODEV;
10201da177e4SLinus Torvalds 		if (filp->f_flags & O_NONBLOCK) {
1021939647eeSJames Bottomley 			if (scsi_host_in_recovery(sdp->device->host))
10221da177e4SLinus Torvalds 				return -EBUSY;
10231da177e4SLinus Torvalds 		} else if (!scsi_block_when_processing_errors(sdp->device))
10241da177e4SLinus Torvalds 			return -EBUSY;
10251da177e4SLinus Torvalds 		result = get_user(val, ip);
10261da177e4SLinus Torvalds 		if (result)
10271da177e4SLinus Torvalds 			return result;
10281da177e4SLinus Torvalds 		if (SG_SCSI_RESET_NOTHING == val)
10291da177e4SLinus Torvalds 			return 0;
10301da177e4SLinus Torvalds 		switch (val) {
10311da177e4SLinus Torvalds 		case SG_SCSI_RESET_DEVICE:
10321da177e4SLinus Torvalds 			val = SCSI_TRY_RESET_DEVICE;
10331da177e4SLinus Torvalds 			break;
103439120e11SBrian King 		case SG_SCSI_RESET_TARGET:
103539120e11SBrian King 			val = SCSI_TRY_RESET_TARGET;
103639120e11SBrian King 			break;
10371da177e4SLinus Torvalds 		case SG_SCSI_RESET_BUS:
10381da177e4SLinus Torvalds 			val = SCSI_TRY_RESET_BUS;
10391da177e4SLinus Torvalds 			break;
10401da177e4SLinus Torvalds 		case SG_SCSI_RESET_HOST:
10411da177e4SLinus Torvalds 			val = SCSI_TRY_RESET_HOST;
10421da177e4SLinus Torvalds 			break;
10431da177e4SLinus Torvalds 		default:
10441da177e4SLinus Torvalds 			return -EINVAL;
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
10471da177e4SLinus Torvalds 			return -EACCES;
10481da177e4SLinus Torvalds 		return (scsi_reset_provider(sdp->device, val) ==
10491da177e4SLinus Torvalds 			SUCCESS) ? 0 : -EIO;
10501da177e4SLinus Torvalds 	case SCSI_IOCTL_SEND_COMMAND:
10511da177e4SLinus Torvalds 		if (sdp->detached)
10521da177e4SLinus Torvalds 			return -ENODEV;
10531da177e4SLinus Torvalds 		if (read_only) {
10541da177e4SLinus Torvalds 			unsigned char opcode = WRITE_6;
10551da177e4SLinus Torvalds 			Scsi_Ioctl_Command __user *siocp = p;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 			if (copy_from_user(&opcode, siocp->data, 1))
10581da177e4SLinus Torvalds 				return -EFAULT;
105914e507b8SFUJITA Tomonori 			if (sg_allow_access(filp, &opcode))
10601da177e4SLinus Torvalds 				return -EPERM;
10611da177e4SLinus Torvalds 		}
1062*e915e872SAl Viro 		return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
10631da177e4SLinus Torvalds 	case SG_SET_DEBUG:
10641da177e4SLinus Torvalds 		result = get_user(val, ip);
10651da177e4SLinus Torvalds 		if (result)
10661da177e4SLinus Torvalds 			return result;
10671da177e4SLinus Torvalds 		sdp->sgdebug = (char) val;
10681da177e4SLinus Torvalds 		return 0;
10691da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_IDLUN:
10701da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_BUS_NUMBER:
10711da177e4SLinus Torvalds 	case SCSI_IOCTL_PROBE_HOST:
10721da177e4SLinus Torvalds 	case SG_GET_TRANSFORM:
10731da177e4SLinus Torvalds 		if (sdp->detached)
10741da177e4SLinus Torvalds 			return -ENODEV;
10751da177e4SLinus Torvalds 		return scsi_ioctl(sdp->device, cmd_in, p);
107644ec9542SAlan Stern 	case BLKSECTGET:
107744ec9542SAlan Stern 		return put_user(sdp->device->request_queue->max_sectors * 512,
107844ec9542SAlan Stern 				ip);
10796da127adSChristof Schmitt 	case BLKTRACESETUP:
10806da127adSChristof Schmitt 		return blk_trace_setup(sdp->device->request_queue,
10816da127adSChristof Schmitt 				       sdp->disk->disk_name,
10826da127adSChristof Schmitt 				       sdp->device->sdev_gendev.devt,
10836da127adSChristof Schmitt 				       (char *)arg);
10846da127adSChristof Schmitt 	case BLKTRACESTART:
10856da127adSChristof Schmitt 		return blk_trace_startstop(sdp->device->request_queue, 1);
10866da127adSChristof Schmitt 	case BLKTRACESTOP:
10876da127adSChristof Schmitt 		return blk_trace_startstop(sdp->device->request_queue, 0);
10886da127adSChristof Schmitt 	case BLKTRACETEARDOWN:
10896da127adSChristof Schmitt 		return blk_trace_remove(sdp->device->request_queue);
10901da177e4SLinus Torvalds 	default:
10911da177e4SLinus Torvalds 		if (read_only)
10921da177e4SLinus Torvalds 			return -EPERM;	/* don't know so take safe approach */
10931da177e4SLinus Torvalds 		return scsi_ioctl(sdp->device, cmd_in, p);
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
10981da177e4SLinus Torvalds static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	Sg_device *sdp;
11011da177e4SLinus Torvalds 	Sg_fd *sfp;
11021da177e4SLinus Torvalds 	struct scsi_device *sdev;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
11051da177e4SLinus Torvalds 		return -ENXIO;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	sdev = sdp->device;
11081da177e4SLinus Torvalds 	if (sdev->host->hostt->compat_ioctl) {
11091da177e4SLinus Torvalds 		int ret;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 		return ret;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds #endif
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds static unsigned int
11211da177e4SLinus Torvalds sg_poll(struct file *filp, poll_table * wait)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	unsigned int res = 0;
11241da177e4SLinus Torvalds 	Sg_device *sdp;
11251da177e4SLinus Torvalds 	Sg_fd *sfp;
11261da177e4SLinus Torvalds 	Sg_request *srp;
11271da177e4SLinus Torvalds 	int count = 0;
11281da177e4SLinus Torvalds 	unsigned long iflags;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))
11311da177e4SLinus Torvalds 	    || sfp->closed)
11321da177e4SLinus Torvalds 		return POLLERR;
11331da177e4SLinus Torvalds 	poll_wait(filp, &sfp->read_wait, wait);
11341da177e4SLinus Torvalds 	read_lock_irqsave(&sfp->rq_list_lock, iflags);
11351da177e4SLinus Torvalds 	for (srp = sfp->headrp; srp; srp = srp->nextrp) {
11361da177e4SLinus Torvalds 		/* if any read waiting, flag it */
11371da177e4SLinus Torvalds 		if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
11381da177e4SLinus Torvalds 			res = POLLIN | POLLRDNORM;
11391da177e4SLinus Torvalds 		++count;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (sdp->detached)
11441da177e4SLinus Torvalds 		res |= POLLHUP;
11451da177e4SLinus Torvalds 	else if (!sfp->cmd_q) {
11461da177e4SLinus Torvalds 		if (0 == count)
11471da177e4SLinus Torvalds 			res |= POLLOUT | POLLWRNORM;
11481da177e4SLinus Torvalds 	} else if (count < SG_MAX_QUEUE)
11491da177e4SLinus Torvalds 		res |= POLLOUT | POLLWRNORM;
11501da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_poll: %s, res=0x%x\n",
11511da177e4SLinus Torvalds 				   sdp->disk->disk_name, (int) res));
11521da177e4SLinus Torvalds 	return res;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds static int
11561da177e4SLinus Torvalds sg_fasync(int fd, struct file *filp, int mode)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	int retval;
11591da177e4SLinus Torvalds 	Sg_device *sdp;
11601da177e4SLinus Torvalds 	Sg_fd *sfp;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
11631da177e4SLinus Torvalds 		return -ENXIO;
11641da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_fasync: %s, mode=%d\n",
11651da177e4SLinus Torvalds 				   sdp->disk->disk_name, mode));
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	retval = fasync_helper(fd, filp, mode, &sfp->async_qp);
11681da177e4SLinus Torvalds 	return (retval < 0) ? retval : 0;
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
1171a13ff0bbSNick Piggin static int
1172a13ff0bbSNick Piggin sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	Sg_fd *sfp;
1175d6b10348SMike Christie 	unsigned long offset, len, sa;
11761da177e4SLinus Torvalds 	Sg_scatter_hold *rsv_schp;
117710db10d1SFUJITA Tomonori 	int k, length;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
1180a13ff0bbSNick Piggin 		return VM_FAULT_SIGBUS;
11811da177e4SLinus Torvalds 	rsv_schp = &sfp->reserve;
1182a13ff0bbSNick Piggin 	offset = vmf->pgoff << PAGE_SHIFT;
11831da177e4SLinus Torvalds 	if (offset >= rsv_schp->bufflen)
1184a13ff0bbSNick Piggin 		return VM_FAULT_SIGBUS;
1185a13ff0bbSNick Piggin 	SCSI_LOG_TIMEOUT(3, printk("sg_vma_fault: offset=%lu, scatg=%d\n",
11861da177e4SLinus Torvalds 				   offset, rsv_schp->k_use_sg));
1187d6b10348SMike Christie 	sa = vma->vm_start;
118810db10d1SFUJITA Tomonori 	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
118910db10d1SFUJITA Tomonori 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
11901da177e4SLinus Torvalds 		len = vma->vm_end - sa;
119110db10d1SFUJITA Tomonori 		len = (len < length) ? len : length;
11921da177e4SLinus Torvalds 		if (offset < len) {
119310db10d1SFUJITA Tomonori 			struct page *page = nth_page(rsv_schp->pages[k],
119410db10d1SFUJITA Tomonori 						     offset >> PAGE_SHIFT);
11951da177e4SLinus Torvalds 			get_page(page);	/* increment page count */
1196a13ff0bbSNick Piggin 			vmf->page = page;
1197a13ff0bbSNick Piggin 			return 0; /* success */
11981da177e4SLinus Torvalds 		}
11991da177e4SLinus Torvalds 		sa += len;
12001da177e4SLinus Torvalds 		offset -= len;
12011da177e4SLinus Torvalds 	}
1202d6b10348SMike Christie 
1203a13ff0bbSNick Piggin 	return VM_FAULT_SIGBUS;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds static struct vm_operations_struct sg_mmap_vm_ops = {
1207a13ff0bbSNick Piggin 	.fault = sg_vma_fault,
12081da177e4SLinus Torvalds };
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static int
12111da177e4SLinus Torvalds sg_mmap(struct file *filp, struct vm_area_struct *vma)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	Sg_fd *sfp;
1214d6b10348SMike Christie 	unsigned long req_sz, len, sa;
12151da177e4SLinus Torvalds 	Sg_scatter_hold *rsv_schp;
121610db10d1SFUJITA Tomonori 	int k, length;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
12191da177e4SLinus Torvalds 		return -ENXIO;
1220cb59e840S 	req_sz = vma->vm_end - vma->vm_start;
12211da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_mmap starting, vm_start=%p, len=%d\n",
12221da177e4SLinus Torvalds 				   (void *) vma->vm_start, (int) req_sz));
12231da177e4SLinus Torvalds 	if (vma->vm_pgoff)
12241da177e4SLinus Torvalds 		return -EINVAL;	/* want no offset */
12251da177e4SLinus Torvalds 	rsv_schp = &sfp->reserve;
12261da177e4SLinus Torvalds 	if (req_sz > rsv_schp->bufflen)
12271da177e4SLinus Torvalds 		return -ENOMEM;	/* cannot map more than reserved buffer */
12281da177e4SLinus Torvalds 
1229d6b10348SMike Christie 	sa = vma->vm_start;
123010db10d1SFUJITA Tomonori 	length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
123110db10d1SFUJITA Tomonori 	for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
12321da177e4SLinus Torvalds 		len = vma->vm_end - sa;
123310db10d1SFUJITA Tomonori 		len = (len < length) ? len : length;
12341da177e4SLinus Torvalds 		sa += len;
12351da177e4SLinus Torvalds 	}
1236d6b10348SMike Christie 
12371da177e4SLinus Torvalds 	sfp->mmap_called = 1;
12381c8e71d7SDouglas Gilbert 	vma->vm_flags |= VM_RESERVED;
12391da177e4SLinus Torvalds 	vma->vm_private_data = sfp;
12401da177e4SLinus Torvalds 	vma->vm_ops = &sg_mmap_vm_ops;
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
1244a91a3a20SFUJITA Tomonori /*
1245a91a3a20SFUJITA Tomonori  * This function is a "bottom half" handler that is called by the mid
1246a91a3a20SFUJITA Tomonori  * level when a command is completed (or has failed).
1247a91a3a20SFUJITA Tomonori  */
1248a91a3a20SFUJITA Tomonori static void sg_rq_end_io(struct request *rq, int uptodate)
12491da177e4SLinus Torvalds {
1250a91a3a20SFUJITA Tomonori 	struct sg_request *srp = rq->end_io_data;
12511da177e4SLinus Torvalds 	Sg_device *sdp = NULL;
12521da177e4SLinus Torvalds 	Sg_fd *sfp;
12531da177e4SLinus Torvalds 	unsigned long iflags;
1254cb59e840S 	unsigned int ms;
1255a91a3a20SFUJITA Tomonori 	char *sense;
1256a91a3a20SFUJITA Tomonori 	int result, resid;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (NULL == srp) {
12591da177e4SLinus Torvalds 		printk(KERN_ERR "sg_cmd_done: NULL request\n");
12601da177e4SLinus Torvalds 		return;
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	sfp = srp->parentfp;
12631da177e4SLinus Torvalds 	if (sfp)
12641da177e4SLinus Torvalds 		sdp = sfp->parentdp;
12651da177e4SLinus Torvalds 	if ((NULL == sdp) || sdp->detached) {
12661da177e4SLinus Torvalds 		printk(KERN_INFO "sg_cmd_done: device detached\n");
12671da177e4SLinus Torvalds 		return;
12681da177e4SLinus Torvalds 	}
12691da177e4SLinus Torvalds 
1270a91a3a20SFUJITA Tomonori 	sense = rq->sense;
1271a91a3a20SFUJITA Tomonori 	result = rq->errors;
1272a91a3a20SFUJITA Tomonori 	resid = rq->data_len;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_cmd_done: %s, pack_id=%d, res=0x%x\n",
1275d6b10348SMike Christie 		sdp->disk->disk_name, srp->header.pack_id, result));
1276d6b10348SMike Christie 	srp->header.resid = resid;
1277cb59e840S 	ms = jiffies_to_msecs(jiffies);
1278cb59e840S 	srp->header.duration = (ms > srp->header.duration) ?
1279cb59e840S 				(ms - srp->header.duration) : 0;
1280d6b10348SMike Christie 	if (0 != result) {
12811da177e4SLinus Torvalds 		struct scsi_sense_hdr sshdr;
12821da177e4SLinus Torvalds 
1283d6b10348SMike Christie 		srp->header.status = 0xff & result;
1284d6b10348SMike Christie 		srp->header.masked_status = status_byte(result);
1285d6b10348SMike Christie 		srp->header.msg_status = msg_byte(result);
1286d6b10348SMike Christie 		srp->header.host_status = host_byte(result);
1287d6b10348SMike Christie 		srp->header.driver_status = driver_byte(result);
12881da177e4SLinus Torvalds 		if ((sdp->sgdebug > 0) &&
12891da177e4SLinus Torvalds 		    ((CHECK_CONDITION == srp->header.masked_status) ||
12901da177e4SLinus Torvalds 		     (COMMAND_TERMINATED == srp->header.masked_status)))
1291d6b10348SMike Christie 			__scsi_print_sense("sg_cmd_done", sense,
1292d6b10348SMike Christie 					   SCSI_SENSE_BUFFERSIZE);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		/* Following if statement is a patch supplied by Eric Youngdale */
1295d6b10348SMike Christie 		if (driver_byte(result) != 0
1296d6b10348SMike Christie 		    && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
12971da177e4SLinus Torvalds 		    && !scsi_sense_is_deferred(&sshdr)
12981da177e4SLinus Torvalds 		    && sshdr.sense_key == UNIT_ATTENTION
12991da177e4SLinus Torvalds 		    && sdp->device->removable) {
13001da177e4SLinus Torvalds 			/* Detected possible disc change. Set the bit - this */
13011da177e4SLinus Torvalds 			/* may be used if there are filesystems using this device */
13021da177e4SLinus Torvalds 			sdp->device->changed = 1;
13031da177e4SLinus Torvalds 		}
13041da177e4SLinus Torvalds 	}
13051da177e4SLinus Torvalds 	/* Rely on write phase to clean out srp status values, so no "else" */
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	if (sfp->closed) {	/* whoops this fd already released, cleanup */
13081da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n"));
13091da177e4SLinus Torvalds 		sg_finish_rem_req(srp);
13101da177e4SLinus Torvalds 		srp = NULL;
13111da177e4SLinus Torvalds 		if (NULL == sfp->headrp) {
13127ca63cb4SDouglas Gilbert 			SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
13131da177e4SLinus Torvalds 			if (0 == sg_remove_sfp(sdp, sfp)) {	/* device still present */
13141da177e4SLinus Torvalds 				scsi_device_put(sdp->device);
13151da177e4SLinus Torvalds 			}
13161da177e4SLinus Torvalds 			sfp = NULL;
13171da177e4SLinus Torvalds 		}
13181da177e4SLinus Torvalds 	} else if (srp && srp->orphan) {
13191da177e4SLinus Torvalds 		if (sfp->keep_orphan)
13201da177e4SLinus Torvalds 			srp->sg_io_owned = 0;
13211da177e4SLinus Torvalds 		else {
13221da177e4SLinus Torvalds 			sg_finish_rem_req(srp);
13231da177e4SLinus Torvalds 			srp = NULL;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 	if (sfp && srp) {
13271da177e4SLinus Torvalds 		/* Now wake up any sg_read() that is waiting for this packet. */
13281da177e4SLinus Torvalds 		kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
13291da177e4SLinus Torvalds 		write_lock_irqsave(&sfp->rq_list_lock, iflags);
13301da177e4SLinus Torvalds 		srp->done = 1;
13311da177e4SLinus Torvalds 		wake_up_interruptible(&sfp->read_wait);
13321da177e4SLinus Torvalds 		write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
13331da177e4SLinus Torvalds 	}
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds static struct file_operations sg_fops = {
13371da177e4SLinus Torvalds 	.owner = THIS_MODULE,
13381da177e4SLinus Torvalds 	.read = sg_read,
13391da177e4SLinus Torvalds 	.write = sg_write,
13401da177e4SLinus Torvalds 	.poll = sg_poll,
13411da177e4SLinus Torvalds 	.ioctl = sg_ioctl,
13421da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
13431da177e4SLinus Torvalds 	.compat_ioctl = sg_compat_ioctl,
13441da177e4SLinus Torvalds #endif
13451da177e4SLinus Torvalds 	.open = sg_open,
13461da177e4SLinus Torvalds 	.mmap = sg_mmap,
13471da177e4SLinus Torvalds 	.release = sg_release,
13481da177e4SLinus Torvalds 	.fasync = sg_fasync,
13491da177e4SLinus Torvalds };
13501da177e4SLinus Torvalds 
1351d253878bSgregkh@suse.de static struct class *sg_sysfs_class;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds static int sg_sysfs_valid = 0;
13541da177e4SLinus Torvalds 
13557c07d613SJames Bottomley static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
13561da177e4SLinus Torvalds {
1357d6b10348SMike Christie 	struct request_queue *q = scsidp->request_queue;
13581da177e4SLinus Torvalds 	Sg_device *sdp;
13591da177e4SLinus Torvalds 	unsigned long iflags;
13607c07d613SJames Bottomley 	int error;
13617c07d613SJames Bottomley 	u32 k;
13621da177e4SLinus Torvalds 
136324669f75SJes Sorensen 	sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
13641da177e4SLinus Torvalds 	if (!sdp) {
13651da177e4SLinus Torvalds 		printk(KERN_WARNING "kmalloc Sg_device failure\n");
13667c07d613SJames Bottomley 		return ERR_PTR(-ENOMEM);
13677c07d613SJames Bottomley 	}
13687c07d613SJames Bottomley 	error = -ENOMEM;
13697c07d613SJames Bottomley 	if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
13707c07d613SJames Bottomley 		printk(KERN_WARNING "idr expansion Sg_device failure\n");
13717c07d613SJames Bottomley 		goto out;
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 
13747c07d613SJames Bottomley 	write_lock_irqsave(&sg_index_lock, iflags);
13757c07d613SJames Bottomley 	error = idr_get_new(&sg_index_idr, sdp, &k);
13767c07d613SJames Bottomley 	write_unlock_irqrestore(&sg_index_lock, iflags);
13771da177e4SLinus Torvalds 
13787c07d613SJames Bottomley 	if (error) {
13797c07d613SJames Bottomley 		printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
13807c07d613SJames Bottomley 		       error);
13817c07d613SJames Bottomley 		goto out;
13821da177e4SLinus Torvalds 	}
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	if (unlikely(k >= SG_MAX_DEVS))
13851da177e4SLinus Torvalds 		goto overflow;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
13881da177e4SLinus Torvalds 	sprintf(disk->disk_name, "sg%d", k);
13891da177e4SLinus Torvalds 	disk->first_minor = k;
13901da177e4SLinus Torvalds 	sdp->disk = disk;
13911da177e4SLinus Torvalds 	sdp->device = scsidp;
13921da177e4SLinus Torvalds 	init_waitqueue_head(&sdp->o_excl_wait);
1393d6b10348SMike Christie 	sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
13947c07d613SJames Bottomley 	sdp->index = k;
13951da177e4SLinus Torvalds 
13967c07d613SJames Bottomley 	error = 0;
13971da177e4SLinus Torvalds  out:
13987c07d613SJames Bottomley 	if (error) {
13991da177e4SLinus Torvalds 		kfree(sdp);
14007c07d613SJames Bottomley 		return ERR_PTR(error);
14017c07d613SJames Bottomley 	}
14027c07d613SJames Bottomley 	return sdp;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds  overflow:
14059ccfc756SJames Bottomley 	sdev_printk(KERN_WARNING, scsidp,
14069ccfc756SJames Bottomley 		    "Unable to attach sg device type=%d, minor "
14079ccfc756SJames Bottomley 		    "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
14081da177e4SLinus Torvalds 	error = -ENODEV;
14091da177e4SLinus Torvalds 	goto out;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds static int
1413ee959b00STony Jones sg_add(struct device *cl_dev, struct class_interface *cl_intf)
14141da177e4SLinus Torvalds {
1415ee959b00STony Jones 	struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
14161da177e4SLinus Torvalds 	struct gendisk *disk;
14171da177e4SLinus Torvalds 	Sg_device *sdp = NULL;
14181da177e4SLinus Torvalds 	struct cdev * cdev = NULL;
14197c07d613SJames Bottomley 	int error;
1420454e8957SIshai Rabinovitz 	unsigned long iflags;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	disk = alloc_disk(1);
14231da177e4SLinus Torvalds 	if (!disk) {
14241da177e4SLinus Torvalds 		printk(KERN_WARNING "alloc_disk failed\n");
14251da177e4SLinus Torvalds 		return -ENOMEM;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 	disk->major = SCSI_GENERIC_MAJOR;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	error = -ENOMEM;
14301da177e4SLinus Torvalds 	cdev = cdev_alloc();
14311da177e4SLinus Torvalds 	if (!cdev) {
14321da177e4SLinus Torvalds 		printk(KERN_WARNING "cdev_alloc failed\n");
14331da177e4SLinus Torvalds 		goto out;
14341da177e4SLinus Torvalds 	}
14351da177e4SLinus Torvalds 	cdev->owner = THIS_MODULE;
14361da177e4SLinus Torvalds 	cdev->ops = &sg_fops;
14371da177e4SLinus Torvalds 
14387c07d613SJames Bottomley 	sdp = sg_alloc(disk, scsidp);
14397c07d613SJames Bottomley 	if (IS_ERR(sdp)) {
14401da177e4SLinus Torvalds 		printk(KERN_WARNING "sg_alloc failed\n");
14417c07d613SJames Bottomley 		error = PTR_ERR(sdp);
14421da177e4SLinus Torvalds 		goto out;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
14457c07d613SJames Bottomley 	error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
14465e3c34c1SGreg KH 	if (error)
1447454e8957SIshai Rabinovitz 		goto cdev_add_err;
14485e3c34c1SGreg KH 
14491da177e4SLinus Torvalds 	sdp->cdev = cdev;
14501da177e4SLinus Torvalds 	if (sg_sysfs_valid) {
1451ee959b00STony Jones 		struct device *sg_class_member;
14521da177e4SLinus Torvalds 
1453d73a1a67SGreg Kroah-Hartman 		sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
1454ee959b00STony Jones 						MKDEV(SCSI_GENERIC_MAJOR,
1455ee959b00STony Jones 						      sdp->index),
1456d73a1a67SGreg Kroah-Hartman 						sdp, "%s", disk->disk_name);
1457d07e0361SFUJITA Tomonori 		if (IS_ERR(sg_class_member)) {
1458d07e0361SFUJITA Tomonori 			printk(KERN_ERR "sg_add: "
1459ee959b00STony Jones 			       "device_create failed\n");
1460d07e0361SFUJITA Tomonori 			error = PTR_ERR(sg_class_member);
1461d07e0361SFUJITA Tomonori 			goto cdev_add_err;
1462d07e0361SFUJITA Tomonori 		}
14631da177e4SLinus Torvalds 		error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
14641da177e4SLinus Torvalds 					  &sg_class_member->kobj, "generic");
14651da177e4SLinus Torvalds 		if (error)
14661da177e4SLinus Torvalds 			printk(KERN_ERR "sg_add: unable to make symlink "
14677c07d613SJames Bottomley 					"'generic' back to sg%d\n", sdp->index);
14681da177e4SLinus Torvalds 	} else
14697c07d613SJames Bottomley 		printk(KERN_WARNING "sg_add: sg_sys Invalid\n");
14701da177e4SLinus Torvalds 
14719ccfc756SJames Bottomley 	sdev_printk(KERN_NOTICE, scsidp,
14727c07d613SJames Bottomley 		    "Attached scsi generic sg%d type %d\n", sdp->index,
14737c07d613SJames Bottomley 		    scsidp->type);
14741da177e4SLinus Torvalds 
1475ee959b00STony Jones 	dev_set_drvdata(cl_dev, sdp);
1476a24484f2SFUJITA Tomonori 
14771da177e4SLinus Torvalds 	return 0;
14781da177e4SLinus Torvalds 
1479454e8957SIshai Rabinovitz cdev_add_err:
14807c07d613SJames Bottomley 	write_lock_irqsave(&sg_index_lock, iflags);
14817c07d613SJames Bottomley 	idr_remove(&sg_index_idr, sdp->index);
14827c07d613SJames Bottomley 	write_unlock_irqrestore(&sg_index_lock, iflags);
14837c07d613SJames Bottomley 	kfree(sdp);
1484454e8957SIshai Rabinovitz 
14851da177e4SLinus Torvalds out:
14861da177e4SLinus Torvalds 	put_disk(disk);
14871da177e4SLinus Torvalds 	if (cdev)
14881da177e4SLinus Torvalds 		cdev_del(cdev);
14891da177e4SLinus Torvalds 	return error;
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds static void
1493ee959b00STony Jones sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
14941da177e4SLinus Torvalds {
1495ee959b00STony Jones 	struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1496ee959b00STony Jones 	Sg_device *sdp = dev_get_drvdata(cl_dev);
14971da177e4SLinus Torvalds 	unsigned long iflags;
14981da177e4SLinus Torvalds 	Sg_fd *sfp;
14991da177e4SLinus Torvalds 	Sg_fd *tsfp;
15001da177e4SLinus Torvalds 	Sg_request *srp;
15011da177e4SLinus Torvalds 	Sg_request *tsrp;
15027c07d613SJames Bottomley 	int delay;
15031da177e4SLinus Torvalds 
15047c07d613SJames Bottomley 	if (!sdp)
15051da177e4SLinus Torvalds 		return;
15067c07d613SJames Bottomley 
15071da177e4SLinus Torvalds 	delay = 0;
15087c07d613SJames Bottomley 	write_lock_irqsave(&sg_index_lock, iflags);
15091da177e4SLinus Torvalds 	if (sdp->headfp) {
15101da177e4SLinus Torvalds 		sdp->detached = 1;
15111da177e4SLinus Torvalds 		for (sfp = sdp->headfp; sfp; sfp = tsfp) {
15121da177e4SLinus Torvalds 			tsfp = sfp->nextfp;
15131da177e4SLinus Torvalds 			for (srp = sfp->headrp; srp; srp = tsrp) {
15141da177e4SLinus Torvalds 				tsrp = srp->nextrp;
15151da177e4SLinus Torvalds 				if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
15161da177e4SLinus Torvalds 					sg_finish_rem_req(srp);
15171da177e4SLinus Torvalds 			}
15181da177e4SLinus Torvalds 			if (sfp->closed) {
15191da177e4SLinus Torvalds 				scsi_device_put(sdp->device);
15201da177e4SLinus Torvalds 				__sg_remove_sfp(sdp, sfp);
15211da177e4SLinus Torvalds 			} else {
15221da177e4SLinus Torvalds 				delay = 1;
15231da177e4SLinus Torvalds 				wake_up_interruptible(&sfp->read_wait);
15241da177e4SLinus Torvalds 				kill_fasync(&sfp->async_qp, SIGPOLL,
15251da177e4SLinus Torvalds 					    POLL_HUP);
15261da177e4SLinus Torvalds 			}
15271da177e4SLinus Torvalds 		}
15287c07d613SJames Bottomley 		SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
15291da177e4SLinus Torvalds 		if (NULL == sdp->headfp) {
15307c07d613SJames Bottomley 			idr_remove(&sg_index_idr, sdp->index);
15311da177e4SLinus Torvalds 		}
15321da177e4SLinus Torvalds 	} else {	/* nothing active, simple case */
15337c07d613SJames Bottomley 		SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
15347c07d613SJames Bottomley 		idr_remove(&sg_index_idr, sdp->index);
15351da177e4SLinus Torvalds 	}
15367c07d613SJames Bottomley 	write_unlock_irqrestore(&sg_index_lock, iflags);
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
1539ee959b00STony Jones 	device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
15401da177e4SLinus Torvalds 	cdev_del(sdp->cdev);
15411da177e4SLinus Torvalds 	sdp->cdev = NULL;
15421da177e4SLinus Torvalds 	put_disk(sdp->disk);
15431da177e4SLinus Torvalds 	sdp->disk = NULL;
15441da177e4SLinus Torvalds 	if (NULL == sdp->headfp)
15457c07d613SJames Bottomley 		kfree(sdp);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	if (delay)
15481da177e4SLinus Torvalds 		msleep(10);	/* dirty detach so delay device destruction */
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
15516460e75aSDouglas Gilbert module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
15526460e75aSDouglas Gilbert module_param_named(def_reserved_size, def_reserved_size, int,
15536460e75aSDouglas Gilbert 		   S_IRUGO | S_IWUSR);
15541da177e4SLinus Torvalds module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds MODULE_AUTHOR("Douglas Gilbert");
15571da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI generic (sg) driver");
15581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
15591da177e4SLinus Torvalds MODULE_VERSION(SG_VERSION_STR);
1560f018fa55SRene Herman MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
15611da177e4SLinus Torvalds 
15626460e75aSDouglas Gilbert MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
15636460e75aSDouglas Gilbert                 "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
15641da177e4SLinus Torvalds MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
15651da177e4SLinus Torvalds MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds static int __init
15681da177e4SLinus Torvalds init_sg(void)
15691da177e4SLinus Torvalds {
15701da177e4SLinus Torvalds 	int rc;
15711da177e4SLinus Torvalds 
15726460e75aSDouglas Gilbert 	if (scatter_elem_sz < PAGE_SIZE) {
15736460e75aSDouglas Gilbert 		scatter_elem_sz = PAGE_SIZE;
15746460e75aSDouglas Gilbert 		scatter_elem_sz_prev = scatter_elem_sz;
15756460e75aSDouglas Gilbert 	}
15761da177e4SLinus Torvalds 	if (def_reserved_size >= 0)
15771da177e4SLinus Torvalds 		sg_big_buff = def_reserved_size;
15786460e75aSDouglas Gilbert 	else
15796460e75aSDouglas Gilbert 		def_reserved_size = sg_big_buff;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
15821da177e4SLinus Torvalds 				    SG_MAX_DEVS, "sg");
15831da177e4SLinus Torvalds 	if (rc)
15841da177e4SLinus Torvalds 		return rc;
1585d253878bSgregkh@suse.de         sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
15861da177e4SLinus Torvalds         if ( IS_ERR(sg_sysfs_class) ) {
15871da177e4SLinus Torvalds 		rc = PTR_ERR(sg_sysfs_class);
15881da177e4SLinus Torvalds 		goto err_out;
15891da177e4SLinus Torvalds         }
15901da177e4SLinus Torvalds 	sg_sysfs_valid = 1;
15911da177e4SLinus Torvalds 	rc = scsi_register_interface(&sg_interface);
15921da177e4SLinus Torvalds 	if (0 == rc) {
15931da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
15941da177e4SLinus Torvalds 		sg_proc_init();
15951da177e4SLinus Torvalds #endif				/* CONFIG_SCSI_PROC_FS */
15961da177e4SLinus Torvalds 		return 0;
15971da177e4SLinus Torvalds 	}
1598d253878bSgregkh@suse.de 	class_destroy(sg_sysfs_class);
15991da177e4SLinus Torvalds err_out:
16001da177e4SLinus Torvalds 	unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
16011da177e4SLinus Torvalds 	return rc;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds static void __exit
16051da177e4SLinus Torvalds exit_sg(void)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
16081da177e4SLinus Torvalds 	sg_proc_cleanup();
16091da177e4SLinus Torvalds #endif				/* CONFIG_SCSI_PROC_FS */
16101da177e4SLinus Torvalds 	scsi_unregister_interface(&sg_interface);
1611d253878bSgregkh@suse.de 	class_destroy(sg_sysfs_class);
16121da177e4SLinus Torvalds 	sg_sysfs_valid = 0;
16131da177e4SLinus Torvalds 	unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
16141da177e4SLinus Torvalds 				 SG_MAX_DEVS);
16157c07d613SJames Bottomley 	idr_destroy(&sg_index_idr);
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
161844c7b0eaSFUJITA Tomonori static int sg_start_req(Sg_request *srp, unsigned char *cmd)
161910865dfaSFUJITA Tomonori {
1620626710c9SFUJITA Tomonori 	int res;
162110865dfaSFUJITA Tomonori 	struct request *rq;
162244c7b0eaSFUJITA Tomonori 	Sg_fd *sfp = srp->parentfp;
162344c7b0eaSFUJITA Tomonori 	sg_io_hdr_t *hp = &srp->header;
162444c7b0eaSFUJITA Tomonori 	int dxfer_len = (int) hp->dxfer_len;
162544c7b0eaSFUJITA Tomonori 	int dxfer_dir = hp->dxfer_direction;
1626626710c9SFUJITA Tomonori 	unsigned int iov_count = hp->iovec_count;
162744c7b0eaSFUJITA Tomonori 	Sg_scatter_hold *req_schp = &srp->data;
162844c7b0eaSFUJITA Tomonori 	Sg_scatter_hold *rsv_schp = &sfp->reserve;
162944c7b0eaSFUJITA Tomonori 	struct request_queue *q = sfp->parentdp->device->request_queue;
1630626710c9SFUJITA Tomonori 	struct rq_map_data *md, map_data;
163110865dfaSFUJITA Tomonori 	int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
163210865dfaSFUJITA Tomonori 
163344c7b0eaSFUJITA Tomonori 	SCSI_LOG_TIMEOUT(4, printk(KERN_INFO "sg_start_req: dxfer_len=%d\n",
163444c7b0eaSFUJITA Tomonori 				   dxfer_len));
163544c7b0eaSFUJITA Tomonori 
163610865dfaSFUJITA Tomonori 	rq = blk_get_request(q, rw, GFP_ATOMIC);
163710865dfaSFUJITA Tomonori 	if (!rq)
163810865dfaSFUJITA Tomonori 		return -ENOMEM;
163910865dfaSFUJITA Tomonori 
164010865dfaSFUJITA Tomonori 	memcpy(rq->cmd, cmd, hp->cmd_len);
164110865dfaSFUJITA Tomonori 
164210865dfaSFUJITA Tomonori 	rq->cmd_len = hp->cmd_len;
164310865dfaSFUJITA Tomonori 	rq->cmd_type = REQ_TYPE_BLOCK_PC;
164410865dfaSFUJITA Tomonori 
164510865dfaSFUJITA Tomonori 	srp->rq = rq;
164610865dfaSFUJITA Tomonori 	rq->end_io_data = srp;
164710865dfaSFUJITA Tomonori 	rq->sense = srp->sense_b;
164810865dfaSFUJITA Tomonori 	rq->retries = SG_DEFAULT_RETRIES;
164910865dfaSFUJITA Tomonori 
16501da177e4SLinus Torvalds 	if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
165110db10d1SFUJITA Tomonori 		return 0;
165210865dfaSFUJITA Tomonori 
1653626710c9SFUJITA Tomonori 	if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
1654626710c9SFUJITA Tomonori 	    dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
1655626710c9SFUJITA Tomonori 	    !sfp->parentdp->device->host->unchecked_isa_dma &&
165601cfcdddSFUJITA Tomonori 	    blk_rq_aligned(q, hp->dxferp, dxfer_len))
1657626710c9SFUJITA Tomonori 		md = NULL;
1658626710c9SFUJITA Tomonori 	else
1659626710c9SFUJITA Tomonori 		md = &map_data;
16607e56cb0fSFUJITA Tomonori 
1661626710c9SFUJITA Tomonori 	if (md) {
1662626710c9SFUJITA Tomonori 		if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
16631da177e4SLinus Torvalds 			sg_link_reserve(sfp, srp, dxfer_len);
1664626710c9SFUJITA Tomonori 		else {
16651da177e4SLinus Torvalds 			res = sg_build_indirect(req_schp, sfp, dxfer_len);
1666626710c9SFUJITA Tomonori 			if (res)
1667626710c9SFUJITA Tomonori 				return res;
166810db10d1SFUJITA Tomonori 		}
166910db10d1SFUJITA Tomonori 
1670626710c9SFUJITA Tomonori 		md->pages = req_schp->pages;
1671626710c9SFUJITA Tomonori 		md->page_order = req_schp->page_order;
1672626710c9SFUJITA Tomonori 		md->nr_entries = req_schp->k_use_sg;
1673626710c9SFUJITA Tomonori 	}
1674626710c9SFUJITA Tomonori 
1675626710c9SFUJITA Tomonori 	if (iov_count)
1676626710c9SFUJITA Tomonori 		res = blk_rq_map_user_iov(q, rq, md, hp->dxferp, iov_count,
1677626710c9SFUJITA Tomonori 					  hp->dxfer_len, GFP_ATOMIC);
1678626710c9SFUJITA Tomonori 	else
1679626710c9SFUJITA Tomonori 		res = blk_rq_map_user(q, rq, md, hp->dxferp,
1680626710c9SFUJITA Tomonori 				      hp->dxfer_len, GFP_ATOMIC);
1681626710c9SFUJITA Tomonori 
1682626710c9SFUJITA Tomonori 	if (!res) {
1683626710c9SFUJITA Tomonori 		srp->bio = rq->bio;
1684626710c9SFUJITA Tomonori 
1685626710c9SFUJITA Tomonori 		if (!md) {
1686626710c9SFUJITA Tomonori 			req_schp->dio_in_use = 1;
1687626710c9SFUJITA Tomonori 			hp->info |= SG_INFO_DIRECT_IO;
1688626710c9SFUJITA Tomonori 		}
1689626710c9SFUJITA Tomonori 	}
16901da177e4SLinus Torvalds 	return res;
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds static void
16941da177e4SLinus Torvalds sg_finish_rem_req(Sg_request * srp)
16951da177e4SLinus Torvalds {
16961da177e4SLinus Torvalds 	Sg_fd *sfp = srp->parentfp;
16971da177e4SLinus Torvalds 	Sg_scatter_hold *req_schp = &srp->data;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_finish_rem_req: res_used=%d\n", (int) srp->res_used));
17001da177e4SLinus Torvalds 	if (srp->res_used)
17011da177e4SLinus Torvalds 		sg_unlink_reserve(sfp, srp);
17021da177e4SLinus Torvalds 	else
17031da177e4SLinus Torvalds 		sg_remove_scat(req_schp);
170410865dfaSFUJITA Tomonori 
17056e5a30cbSFUJITA Tomonori 	if (srp->rq) {
17066e5a30cbSFUJITA Tomonori 		if (srp->bio)
17076e5a30cbSFUJITA Tomonori 			blk_rq_unmap_user(srp->bio);
170810db10d1SFUJITA Tomonori 
170910865dfaSFUJITA Tomonori 		blk_put_request(srp->rq);
17106e5a30cbSFUJITA Tomonori 	}
171110865dfaSFUJITA Tomonori 
17121da177e4SLinus Torvalds 	sg_remove_request(sfp, srp);
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds static int
17161da177e4SLinus Torvalds sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
17171da177e4SLinus Torvalds {
171810db10d1SFUJITA Tomonori 	int sg_bufflen = tablesize * sizeof(struct page *);
17192d20eaf9SAl Viro 	gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
17201da177e4SLinus Torvalds 
172110db10d1SFUJITA Tomonori 	schp->pages = kzalloc(sg_bufflen, gfp_flags);
172210db10d1SFUJITA Tomonori 	if (!schp->pages)
17231da177e4SLinus Torvalds 		return -ENOMEM;
17241da177e4SLinus Torvalds 	schp->sglist_len = sg_bufflen;
1725d6b10348SMike Christie 	return tablesize;	/* number of scat_gath elements allocated */
17261da177e4SLinus Torvalds }
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds static int
17291da177e4SLinus Torvalds sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
17301da177e4SLinus Torvalds {
173110db10d1SFUJITA Tomonori 	int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
1732d6b10348SMike Christie 	int sg_tablesize = sfp->parentdp->sg_tablesize;
173310db10d1SFUJITA Tomonori 	int blk_size = buff_size, order;
173410db10d1SFUJITA Tomonori 	gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
17351da177e4SLinus Torvalds 
1736fb119935SEric Sesterhenn 	if (blk_size < 0)
17371da177e4SLinus Torvalds 		return -EFAULT;
17381da177e4SLinus Torvalds 	if (0 == blk_size)
17391da177e4SLinus Torvalds 		++blk_size;	/* don't know why */
17401da177e4SLinus Torvalds /* round request up to next highest SG_SECTOR_SZ byte boundary */
17411da177e4SLinus Torvalds 	blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
17421da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
17431da177e4SLinus Torvalds 				   buff_size, blk_size));
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	/* N.B. ret_sz carried into this block ... */
17461da177e4SLinus Torvalds 	mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
17471da177e4SLinus Torvalds 	if (mx_sc_elems < 0)
17481da177e4SLinus Torvalds 		return mx_sc_elems;	/* most likely -ENOMEM */
17491da177e4SLinus Torvalds 
17506460e75aSDouglas Gilbert 	num = scatter_elem_sz;
17516460e75aSDouglas Gilbert 	if (unlikely(num != scatter_elem_sz_prev)) {
17526460e75aSDouglas Gilbert 		if (num < PAGE_SIZE) {
17536460e75aSDouglas Gilbert 			scatter_elem_sz = PAGE_SIZE;
17546460e75aSDouglas Gilbert 			scatter_elem_sz_prev = PAGE_SIZE;
17556460e75aSDouglas Gilbert 		} else
17566460e75aSDouglas Gilbert 			scatter_elem_sz_prev = num;
17576460e75aSDouglas Gilbert 	}
175810db10d1SFUJITA Tomonori 
175910db10d1SFUJITA Tomonori 	if (sfp->low_dma)
176010db10d1SFUJITA Tomonori 		gfp_mask |= GFP_DMA;
176110db10d1SFUJITA Tomonori 
176210db10d1SFUJITA Tomonori 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
176310db10d1SFUJITA Tomonori 		gfp_mask |= __GFP_ZERO;
176410db10d1SFUJITA Tomonori 
176510db10d1SFUJITA Tomonori 	order = get_order(num);
176610db10d1SFUJITA Tomonori retry:
176710db10d1SFUJITA Tomonori 	ret_sz = 1 << (PAGE_SHIFT + order);
176810db10d1SFUJITA Tomonori 
176910db10d1SFUJITA Tomonori 	for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
177010db10d1SFUJITA Tomonori 	     k++, rem_sz -= ret_sz) {
1771d6b10348SMike Christie 
17726460e75aSDouglas Gilbert 		num = (rem_sz > scatter_elem_sz_prev) ?
17736460e75aSDouglas Gilbert 			scatter_elem_sz_prev : rem_sz;
177410db10d1SFUJITA Tomonori 
177510db10d1SFUJITA Tomonori 		schp->pages[k] = alloc_pages(gfp_mask, order);
177610db10d1SFUJITA Tomonori 		if (!schp->pages[k])
177710db10d1SFUJITA Tomonori 			goto out;
1778d6b10348SMike Christie 
17796460e75aSDouglas Gilbert 		if (num == scatter_elem_sz_prev) {
17806460e75aSDouglas Gilbert 			if (unlikely(ret_sz > scatter_elem_sz_prev)) {
17816460e75aSDouglas Gilbert 				scatter_elem_sz = ret_sz;
17826460e75aSDouglas Gilbert 				scatter_elem_sz_prev = ret_sz;
17836460e75aSDouglas Gilbert 			}
17846460e75aSDouglas Gilbert 		}
17851da177e4SLinus Torvalds 
17867ca63cb4SDouglas Gilbert 		SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k=%d, num=%d, "
17877ca63cb4SDouglas Gilbert 				 "ret_sz=%d\n", k, num, ret_sz));
17881da177e4SLinus Torvalds 	}		/* end of for loop */
1789d6b10348SMike Christie 
179010db10d1SFUJITA Tomonori 	schp->page_order = order;
17911da177e4SLinus Torvalds 	schp->k_use_sg = k;
17927ca63cb4SDouglas Gilbert 	SCSI_LOG_TIMEOUT(5, printk("sg_build_indirect: k_use_sg=%d, "
17937ca63cb4SDouglas Gilbert 			 "rem_sz=%d\n", k, rem_sz));
1794d6b10348SMike Christie 
17951da177e4SLinus Torvalds 	schp->bufflen = blk_size;
17961da177e4SLinus Torvalds 	if (rem_sz > 0)	/* must have failed */
17971da177e4SLinus Torvalds 		return -ENOMEM;
17981da177e4SLinus Torvalds 	return 0;
179910db10d1SFUJITA Tomonori out:
180010db10d1SFUJITA Tomonori 	for (i = 0; i < k; i++)
180110db10d1SFUJITA Tomonori 		__free_pages(schp->pages[k], order);
180210db10d1SFUJITA Tomonori 
180310db10d1SFUJITA Tomonori 	if (--order >= 0)
180410db10d1SFUJITA Tomonori 		goto retry;
180510db10d1SFUJITA Tomonori 
180610db10d1SFUJITA Tomonori 	return -ENOMEM;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds static void
18101da177e4SLinus Torvalds sg_remove_scat(Sg_scatter_hold * schp)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
181310db10d1SFUJITA Tomonori 	if (schp->pages && schp->sglist_len > 0) {
18146e5a30cbSFUJITA Tomonori 		if (!schp->dio_in_use) {
18151da177e4SLinus Torvalds 			int k;
18161da177e4SLinus Torvalds 
181710db10d1SFUJITA Tomonori 			for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
18181da177e4SLinus Torvalds 				SCSI_LOG_TIMEOUT(5, printk(
181910db10d1SFUJITA Tomonori 				    "sg_remove_scat: k=%d, pg=0x%p\n",
182010db10d1SFUJITA Tomonori 				    k, schp->pages[k]));
182110db10d1SFUJITA Tomonori 				__free_pages(schp->pages[k], schp->page_order);
18221da177e4SLinus Torvalds 			}
18236e5a30cbSFUJITA Tomonori 
182410db10d1SFUJITA Tomonori 			kfree(schp->pages);
1825d6b10348SMike Christie 		}
18266e5a30cbSFUJITA Tomonori 	}
18271da177e4SLinus Torvalds 	memset(schp, 0, sizeof (*schp));
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds static int
18311da177e4SLinus Torvalds sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	Sg_scatter_hold *schp = &srp->data;
1834d6b10348SMike Christie 	int k, num;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_read_oxfer: num_read_xfer=%d\n",
18371da177e4SLinus Torvalds 				   num_read_xfer));
18381da177e4SLinus Torvalds 	if ((!outp) || (num_read_xfer <= 0))
18391da177e4SLinus Torvalds 		return 0;
18401da177e4SLinus Torvalds 
184110db10d1SFUJITA Tomonori 	num = 1 << (PAGE_SHIFT + schp->page_order);
184210db10d1SFUJITA Tomonori 	for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
18431da177e4SLinus Torvalds 		if (num > num_read_xfer) {
184410db10d1SFUJITA Tomonori 			if (__copy_to_user(outp, page_address(schp->pages[k]),
1845d6b10348SMike Christie 					   num_read_xfer))
18461da177e4SLinus Torvalds 				return -EFAULT;
18471da177e4SLinus Torvalds 			break;
18481da177e4SLinus Torvalds 		} else {
184910db10d1SFUJITA Tomonori 			if (__copy_to_user(outp, page_address(schp->pages[k]),
1850d6b10348SMike Christie 					   num))
18511da177e4SLinus Torvalds 				return -EFAULT;
18521da177e4SLinus Torvalds 			num_read_xfer -= num;
18531da177e4SLinus Torvalds 			if (num_read_xfer <= 0)
18541da177e4SLinus Torvalds 				break;
18551da177e4SLinus Torvalds 			outp += num;
18561da177e4SLinus Torvalds 		}
18571da177e4SLinus Torvalds 	}
1858d6b10348SMike Christie 
18591da177e4SLinus Torvalds 	return 0;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds static void
18631da177e4SLinus Torvalds sg_build_reserve(Sg_fd * sfp, int req_size)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	Sg_scatter_hold *schp = &sfp->reserve;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_build_reserve: req_size=%d\n", req_size));
18681da177e4SLinus Torvalds 	do {
18691da177e4SLinus Torvalds 		if (req_size < PAGE_SIZE)
18701da177e4SLinus Torvalds 			req_size = PAGE_SIZE;
18711da177e4SLinus Torvalds 		if (0 == sg_build_indirect(schp, sfp, req_size))
18721da177e4SLinus Torvalds 			return;
18731da177e4SLinus Torvalds 		else
18741da177e4SLinus Torvalds 			sg_remove_scat(schp);
18751da177e4SLinus Torvalds 		req_size >>= 1;	/* divide by 2 */
18761da177e4SLinus Torvalds 	} while (req_size > (PAGE_SIZE / 2));
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds static void
18801da177e4SLinus Torvalds sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
18811da177e4SLinus Torvalds {
18821da177e4SLinus Torvalds 	Sg_scatter_hold *req_schp = &srp->data;
18831da177e4SLinus Torvalds 	Sg_scatter_hold *rsv_schp = &sfp->reserve;
1884d6b10348SMike Christie 	int k, num, rem;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	srp->res_used = 1;
18871da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
1888eca7be5eSBrian King 	rem = size;
18891da177e4SLinus Torvalds 
189010db10d1SFUJITA Tomonori 	num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
189110db10d1SFUJITA Tomonori 	for (k = 0; k < rsv_schp->k_use_sg; k++) {
18921da177e4SLinus Torvalds 		if (rem <= num) {
18931da177e4SLinus Torvalds 			req_schp->k_use_sg = k + 1;
1894d6b10348SMike Christie 			req_schp->sglist_len = rsv_schp->sglist_len;
189510db10d1SFUJITA Tomonori 			req_schp->pages = rsv_schp->pages;
1896d6b10348SMike Christie 
18971da177e4SLinus Torvalds 			req_schp->bufflen = size;
189810db10d1SFUJITA Tomonori 			req_schp->page_order = rsv_schp->page_order;
18991da177e4SLinus Torvalds 			break;
19001da177e4SLinus Torvalds 		} else
19011da177e4SLinus Torvalds 			rem -= num;
19021da177e4SLinus Torvalds 	}
1903d6b10348SMike Christie 
19041da177e4SLinus Torvalds 	if (k >= rsv_schp->k_use_sg)
19051da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(1, printk("sg_link_reserve: BAD size\n"));
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds static void
19091da177e4SLinus Torvalds sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	Sg_scatter_hold *req_schp = &srp->data;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(4, printk("sg_unlink_reserve: req->k_use_sg=%d\n",
19141da177e4SLinus Torvalds 				   (int) req_schp->k_use_sg));
19151da177e4SLinus Torvalds 	req_schp->k_use_sg = 0;
19161da177e4SLinus Torvalds 	req_schp->bufflen = 0;
191710db10d1SFUJITA Tomonori 	req_schp->pages = NULL;
191810db10d1SFUJITA Tomonori 	req_schp->page_order = 0;
19191da177e4SLinus Torvalds 	req_schp->sglist_len = 0;
19201da177e4SLinus Torvalds 	sfp->save_scat_len = 0;
19211da177e4SLinus Torvalds 	srp->res_used = 0;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds static Sg_request *
19251da177e4SLinus Torvalds sg_get_rq_mark(Sg_fd * sfp, int pack_id)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	Sg_request *resp;
19281da177e4SLinus Torvalds 	unsigned long iflags;
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
19311da177e4SLinus Torvalds 	for (resp = sfp->headrp; resp; resp = resp->nextrp) {
19321da177e4SLinus Torvalds 		/* look for requests that are ready + not SG_IO owned */
19331da177e4SLinus Torvalds 		if ((1 == resp->done) && (!resp->sg_io_owned) &&
19341da177e4SLinus Torvalds 		    ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
19351da177e4SLinus Torvalds 			resp->done = 2;	/* guard against other readers */
19361da177e4SLinus Torvalds 			break;
19371da177e4SLinus Torvalds 		}
19381da177e4SLinus Torvalds 	}
19391da177e4SLinus Torvalds 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
19401da177e4SLinus Torvalds 	return resp;
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
19441da177e4SLinus Torvalds static Sg_request *
19451da177e4SLinus Torvalds sg_get_nth_request(Sg_fd * sfp, int nth)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	Sg_request *resp;
19481da177e4SLinus Torvalds 	unsigned long iflags;
19491da177e4SLinus Torvalds 	int k;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	read_lock_irqsave(&sfp->rq_list_lock, iflags);
19521da177e4SLinus Torvalds 	for (k = 0, resp = sfp->headrp; resp && (k < nth);
19531da177e4SLinus Torvalds 	     ++k, resp = resp->nextrp) ;
19541da177e4SLinus Torvalds 	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
19551da177e4SLinus Torvalds 	return resp;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds #endif
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds /* always adds to end of list */
19601da177e4SLinus Torvalds static Sg_request *
19611da177e4SLinus Torvalds sg_add_request(Sg_fd * sfp)
19621da177e4SLinus Torvalds {
19631da177e4SLinus Torvalds 	int k;
19641da177e4SLinus Torvalds 	unsigned long iflags;
19651da177e4SLinus Torvalds 	Sg_request *resp;
19661da177e4SLinus Torvalds 	Sg_request *rp = sfp->req_arr;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
19691da177e4SLinus Torvalds 	resp = sfp->headrp;
19701da177e4SLinus Torvalds 	if (!resp) {
19711da177e4SLinus Torvalds 		memset(rp, 0, sizeof (Sg_request));
19721da177e4SLinus Torvalds 		rp->parentfp = sfp;
19731da177e4SLinus Torvalds 		resp = rp;
19741da177e4SLinus Torvalds 		sfp->headrp = resp;
19751da177e4SLinus Torvalds 	} else {
19761da177e4SLinus Torvalds 		if (0 == sfp->cmd_q)
19771da177e4SLinus Torvalds 			resp = NULL;	/* command queuing disallowed */
19781da177e4SLinus Torvalds 		else {
19791da177e4SLinus Torvalds 			for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
19801da177e4SLinus Torvalds 				if (!rp->parentfp)
19811da177e4SLinus Torvalds 					break;
19821da177e4SLinus Torvalds 			}
19831da177e4SLinus Torvalds 			if (k < SG_MAX_QUEUE) {
19841da177e4SLinus Torvalds 				memset(rp, 0, sizeof (Sg_request));
19851da177e4SLinus Torvalds 				rp->parentfp = sfp;
19861da177e4SLinus Torvalds 				while (resp->nextrp)
19871da177e4SLinus Torvalds 					resp = resp->nextrp;
19881da177e4SLinus Torvalds 				resp->nextrp = rp;
19891da177e4SLinus Torvalds 				resp = rp;
19901da177e4SLinus Torvalds 			} else
19911da177e4SLinus Torvalds 				resp = NULL;
19921da177e4SLinus Torvalds 		}
19931da177e4SLinus Torvalds 	}
19941da177e4SLinus Torvalds 	if (resp) {
19951da177e4SLinus Torvalds 		resp->nextrp = NULL;
1996cb59e840S 		resp->header.duration = jiffies_to_msecs(jiffies);
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
19991da177e4SLinus Torvalds 	return resp;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds /* Return of 1 for found; 0 for not found */
20031da177e4SLinus Torvalds static int
20041da177e4SLinus Torvalds sg_remove_request(Sg_fd * sfp, Sg_request * srp)
20051da177e4SLinus Torvalds {
20061da177e4SLinus Torvalds 	Sg_request *prev_rp;
20071da177e4SLinus Torvalds 	Sg_request *rp;
20081da177e4SLinus Torvalds 	unsigned long iflags;
20091da177e4SLinus Torvalds 	int res = 0;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	if ((!sfp) || (!srp) || (!sfp->headrp))
20121da177e4SLinus Torvalds 		return res;
20131da177e4SLinus Torvalds 	write_lock_irqsave(&sfp->rq_list_lock, iflags);
20141da177e4SLinus Torvalds 	prev_rp = sfp->headrp;
20151da177e4SLinus Torvalds 	if (srp == prev_rp) {
20161da177e4SLinus Torvalds 		sfp->headrp = prev_rp->nextrp;
20171da177e4SLinus Torvalds 		prev_rp->parentfp = NULL;
20181da177e4SLinus Torvalds 		res = 1;
20191da177e4SLinus Torvalds 	} else {
20201da177e4SLinus Torvalds 		while ((rp = prev_rp->nextrp)) {
20211da177e4SLinus Torvalds 			if (srp == rp) {
20221da177e4SLinus Torvalds 				prev_rp->nextrp = rp->nextrp;
20231da177e4SLinus Torvalds 				rp->parentfp = NULL;
20241da177e4SLinus Torvalds 				res = 1;
20251da177e4SLinus Torvalds 				break;
20261da177e4SLinus Torvalds 			}
20271da177e4SLinus Torvalds 			prev_rp = rp;
20281da177e4SLinus Torvalds 		}
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 	write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
20311da177e4SLinus Torvalds 	return res;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
20351da177e4SLinus Torvalds static Sg_fd *
20361da177e4SLinus Torvalds sg_get_nth_sfp(Sg_device * sdp, int nth)
20371da177e4SLinus Torvalds {
20381da177e4SLinus Torvalds 	Sg_fd *resp;
20391da177e4SLinus Torvalds 	unsigned long iflags;
20401da177e4SLinus Torvalds 	int k;
20411da177e4SLinus Torvalds 
20427c07d613SJames Bottomley 	read_lock_irqsave(&sg_index_lock, iflags);
20431da177e4SLinus Torvalds 	for (k = 0, resp = sdp->headfp; resp && (k < nth);
20441da177e4SLinus Torvalds 	     ++k, resp = resp->nextfp) ;
20457c07d613SJames Bottomley 	read_unlock_irqrestore(&sg_index_lock, iflags);
20461da177e4SLinus Torvalds 	return resp;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds #endif
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds static Sg_fd *
20511da177e4SLinus Torvalds sg_add_sfp(Sg_device * sdp, int dev)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	Sg_fd *sfp;
20541da177e4SLinus Torvalds 	unsigned long iflags;
205544ec9542SAlan Stern 	int bufflen;
20561da177e4SLinus Torvalds 
2057d6b10348SMike Christie 	sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
20581da177e4SLinus Torvalds 	if (!sfp)
20591da177e4SLinus Torvalds 		return NULL;
2060d6b10348SMike Christie 
20611da177e4SLinus Torvalds 	init_waitqueue_head(&sfp->read_wait);
20621da177e4SLinus Torvalds 	rwlock_init(&sfp->rq_list_lock);
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	sfp->timeout = SG_DEFAULT_TIMEOUT;
20651da177e4SLinus Torvalds 	sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
20661da177e4SLinus Torvalds 	sfp->force_packid = SG_DEF_FORCE_PACK_ID;
20671da177e4SLinus Torvalds 	sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
20681da177e4SLinus Torvalds 	    sdp->device->host->unchecked_isa_dma : 1;
20691da177e4SLinus Torvalds 	sfp->cmd_q = SG_DEF_COMMAND_Q;
20701da177e4SLinus Torvalds 	sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
20711da177e4SLinus Torvalds 	sfp->parentdp = sdp;
20727c07d613SJames Bottomley 	write_lock_irqsave(&sg_index_lock, iflags);
20731da177e4SLinus Torvalds 	if (!sdp->headfp)
20741da177e4SLinus Torvalds 		sdp->headfp = sfp;
20751da177e4SLinus Torvalds 	else {			/* add to tail of existing list */
20761da177e4SLinus Torvalds 		Sg_fd *pfp = sdp->headfp;
20771da177e4SLinus Torvalds 		while (pfp->nextfp)
20781da177e4SLinus Torvalds 			pfp = pfp->nextfp;
20791da177e4SLinus Torvalds 		pfp->nextfp = sfp;
20801da177e4SLinus Torvalds 	}
20817c07d613SJames Bottomley 	write_unlock_irqrestore(&sg_index_lock, iflags);
20821da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
20836460e75aSDouglas Gilbert 	if (unlikely(sg_big_buff != def_reserved_size))
20846460e75aSDouglas Gilbert 		sg_big_buff = def_reserved_size;
20856460e75aSDouglas Gilbert 
208644ec9542SAlan Stern 	bufflen = min_t(int, sg_big_buff,
208744ec9542SAlan Stern 			sdp->device->request_queue->max_sectors * 512);
208844ec9542SAlan Stern 	sg_build_reserve(sfp, bufflen);
20891da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp:   bufflen=%d, k_use_sg=%d\n",
20901da177e4SLinus Torvalds 			   sfp->reserve.bufflen, sfp->reserve.k_use_sg));
20911da177e4SLinus Torvalds 	return sfp;
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds static void
20951da177e4SLinus Torvalds __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
20961da177e4SLinus Torvalds {
20971da177e4SLinus Torvalds 	Sg_fd *fp;
20981da177e4SLinus Torvalds 	Sg_fd *prev_fp;
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	prev_fp = sdp->headfp;
21011da177e4SLinus Torvalds 	if (sfp == prev_fp)
21021da177e4SLinus Torvalds 		sdp->headfp = prev_fp->nextfp;
21031da177e4SLinus Torvalds 	else {
21041da177e4SLinus Torvalds 		while ((fp = prev_fp->nextfp)) {
21051da177e4SLinus Torvalds 			if (sfp == fp) {
21061da177e4SLinus Torvalds 				prev_fp->nextfp = fp->nextfp;
21071da177e4SLinus Torvalds 				break;
21081da177e4SLinus Torvalds 			}
21091da177e4SLinus Torvalds 			prev_fp = fp;
21101da177e4SLinus Torvalds 		}
21111da177e4SLinus Torvalds 	}
21121da177e4SLinus Torvalds 	if (sfp->reserve.bufflen > 0) {
21131da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(6,
21141da177e4SLinus Torvalds 			printk("__sg_remove_sfp:    bufflen=%d, k_use_sg=%d\n",
21151da177e4SLinus Torvalds 			(int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg));
21161da177e4SLinus Torvalds 		sg_remove_scat(&sfp->reserve);
21171da177e4SLinus Torvalds 	}
21181da177e4SLinus Torvalds 	sfp->parentdp = NULL;
21191da177e4SLinus Torvalds 	SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp:    sfp=0x%p\n", sfp));
2120d6b10348SMike Christie 	kfree(sfp);
21211da177e4SLinus Torvalds }
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds /* Returns 0 in normal case, 1 when detached and sdp object removed */
21241da177e4SLinus Torvalds static int
21251da177e4SLinus Torvalds sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
21261da177e4SLinus Torvalds {
21271da177e4SLinus Torvalds 	Sg_request *srp;
21281da177e4SLinus Torvalds 	Sg_request *tsrp;
21291da177e4SLinus Torvalds 	int dirty = 0;
21301da177e4SLinus Torvalds 	int res = 0;
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	for (srp = sfp->headrp; srp; srp = tsrp) {
21331da177e4SLinus Torvalds 		tsrp = srp->nextrp;
21341da177e4SLinus Torvalds 		if (sg_srp_done(srp, sfp))
21351da177e4SLinus Torvalds 			sg_finish_rem_req(srp);
21361da177e4SLinus Torvalds 		else
21371da177e4SLinus Torvalds 			++dirty;
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds 	if (0 == dirty) {
21401da177e4SLinus Torvalds 		unsigned long iflags;
21411da177e4SLinus Torvalds 
21427c07d613SJames Bottomley 		write_lock_irqsave(&sg_index_lock, iflags);
21431da177e4SLinus Torvalds 		__sg_remove_sfp(sdp, sfp);
21441da177e4SLinus Torvalds 		if (sdp->detached && (NULL == sdp->headfp)) {
21457c07d613SJames Bottomley 			idr_remove(&sg_index_idr, sdp->index);
21467c07d613SJames Bottomley 			kfree(sdp);
21471da177e4SLinus Torvalds 			res = 1;
21481da177e4SLinus Torvalds 		}
21497c07d613SJames Bottomley 		write_unlock_irqrestore(&sg_index_lock, iflags);
21501da177e4SLinus Torvalds 	} else {
21511da177e4SLinus Torvalds 		/* MOD_INC's to inhibit unloading sg and associated adapter driver */
21521da177e4SLinus Torvalds 		/* only bump the access_count if we actually succeeded in
21531da177e4SLinus Torvalds 		 * throwing another counter on the host module */
21541da177e4SLinus Torvalds 		scsi_device_get(sdp->device);	/* XXX: retval ignored? */
21551da177e4SLinus Torvalds 		sfp->closed = 1;	/* flag dirty state on this fd */
21561da177e4SLinus Torvalds 		SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
21571da177e4SLinus Torvalds 				  dirty));
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds 	return res;
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds static int
21631da177e4SLinus Torvalds sg_res_in_use(Sg_fd * sfp)
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	const Sg_request *srp;
21661da177e4SLinus Torvalds 	unsigned long iflags;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	read_lock_irqsave(&sfp->rq_list_lock, iflags);
21691da177e4SLinus Torvalds 	for (srp = sfp->headrp; srp; srp = srp->nextrp)
21701da177e4SLinus Torvalds 		if (srp->res_used)
21711da177e4SLinus Torvalds 			break;
21721da177e4SLinus Torvalds 	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
21731da177e4SLinus Torvalds 	return srp ? 1 : 0;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
21771da177e4SLinus Torvalds static int
21787c07d613SJames Bottomley sg_idr_max_id(int id, void *p, void *data)
21797c07d613SJames Bottomley {
21807c07d613SJames Bottomley 	int *k = data;
21817c07d613SJames Bottomley 
21827c07d613SJames Bottomley 	if (*k < id)
21837c07d613SJames Bottomley 		*k = id;
21847c07d613SJames Bottomley 
21857c07d613SJames Bottomley 	return 0;
21867c07d613SJames Bottomley }
21877c07d613SJames Bottomley 
21887c07d613SJames Bottomley static int
21891da177e4SLinus Torvalds sg_last_dev(void)
21901da177e4SLinus Torvalds {
219153474c04STony Battersby 	int k = -1;
21921da177e4SLinus Torvalds 	unsigned long iflags;
21931da177e4SLinus Torvalds 
21947c07d613SJames Bottomley 	read_lock_irqsave(&sg_index_lock, iflags);
21957c07d613SJames Bottomley 	idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
21967c07d613SJames Bottomley 	read_unlock_irqrestore(&sg_index_lock, iflags);
21971da177e4SLinus Torvalds 	return k + 1;		/* origin 1 */
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds #endif
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds static Sg_device *
22021da177e4SLinus Torvalds sg_get_dev(int dev)
22031da177e4SLinus Torvalds {
22047c07d613SJames Bottomley 	Sg_device *sdp;
22051da177e4SLinus Torvalds 	unsigned long iflags;
22061da177e4SLinus Torvalds 
22077c07d613SJames Bottomley 	read_lock_irqsave(&sg_index_lock, iflags);
22087c07d613SJames Bottomley 	sdp = idr_find(&sg_index_idr, dev);
22097c07d613SJames Bottomley 	read_unlock_irqrestore(&sg_index_lock, iflags);
22107c07d613SJames Bottomley 
22111da177e4SLinus Torvalds 	return sdp;
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds #ifdef CONFIG_SCSI_PROC_FS
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds static struct proc_dir_entry *sg_proc_sgp = NULL;
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds static char sg_proc_sg_dirname[] = "scsi/sg";
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds static int sg_proc_seq_show_int(struct seq_file *s, void *v);
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
22231da177e4SLinus Torvalds static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
22241da177e4SLinus Torvalds 			          size_t count, loff_t *off);
22251da177e4SLinus Torvalds static struct file_operations adio_fops = {
22261da177e4SLinus Torvalds 	/* .owner, .read and .llseek added in sg_proc_init() */
22271da177e4SLinus Torvalds 	.open = sg_proc_single_open_adio,
22281da177e4SLinus Torvalds 	.write = sg_proc_write_adio,
22291da177e4SLinus Torvalds 	.release = single_release,
22301da177e4SLinus Torvalds };
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
22331da177e4SLinus Torvalds static ssize_t sg_proc_write_dressz(struct file *filp,
22341da177e4SLinus Torvalds 		const char __user *buffer, size_t count, loff_t *off);
22351da177e4SLinus Torvalds static struct file_operations dressz_fops = {
22361da177e4SLinus Torvalds 	.open = sg_proc_single_open_dressz,
22371da177e4SLinus Torvalds 	.write = sg_proc_write_dressz,
22381da177e4SLinus Torvalds 	.release = single_release,
22391da177e4SLinus Torvalds };
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds static int sg_proc_seq_show_version(struct seq_file *s, void *v);
22421da177e4SLinus Torvalds static int sg_proc_single_open_version(struct inode *inode, struct file *file);
22431da177e4SLinus Torvalds static struct file_operations version_fops = {
22441da177e4SLinus Torvalds 	.open = sg_proc_single_open_version,
22451da177e4SLinus Torvalds 	.release = single_release,
22461da177e4SLinus Torvalds };
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
22491da177e4SLinus Torvalds static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
22501da177e4SLinus Torvalds static struct file_operations devhdr_fops = {
22511da177e4SLinus Torvalds 	.open = sg_proc_single_open_devhdr,
22521da177e4SLinus Torvalds 	.release = single_release,
22531da177e4SLinus Torvalds };
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
22561da177e4SLinus Torvalds static int sg_proc_open_dev(struct inode *inode, struct file *file);
22571da177e4SLinus Torvalds static void * dev_seq_start(struct seq_file *s, loff_t *pos);
22581da177e4SLinus Torvalds static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
22591da177e4SLinus Torvalds static void dev_seq_stop(struct seq_file *s, void *v);
22601da177e4SLinus Torvalds static struct file_operations dev_fops = {
22611da177e4SLinus Torvalds 	.open = sg_proc_open_dev,
22621da177e4SLinus Torvalds 	.release = seq_release,
22631da177e4SLinus Torvalds };
22641da177e4SLinus Torvalds static struct seq_operations dev_seq_ops = {
22651da177e4SLinus Torvalds 	.start = dev_seq_start,
22661da177e4SLinus Torvalds 	.next  = dev_seq_next,
22671da177e4SLinus Torvalds 	.stop  = dev_seq_stop,
22681da177e4SLinus Torvalds 	.show  = sg_proc_seq_show_dev,
22691da177e4SLinus Torvalds };
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
22721da177e4SLinus Torvalds static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
22731da177e4SLinus Torvalds static struct file_operations devstrs_fops = {
22741da177e4SLinus Torvalds 	.open = sg_proc_open_devstrs,
22751da177e4SLinus Torvalds 	.release = seq_release,
22761da177e4SLinus Torvalds };
22771da177e4SLinus Torvalds static struct seq_operations devstrs_seq_ops = {
22781da177e4SLinus Torvalds 	.start = dev_seq_start,
22791da177e4SLinus Torvalds 	.next  = dev_seq_next,
22801da177e4SLinus Torvalds 	.stop  = dev_seq_stop,
22811da177e4SLinus Torvalds 	.show  = sg_proc_seq_show_devstrs,
22821da177e4SLinus Torvalds };
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
22851da177e4SLinus Torvalds static int sg_proc_open_debug(struct inode *inode, struct file *file);
22861da177e4SLinus Torvalds static struct file_operations debug_fops = {
22871da177e4SLinus Torvalds 	.open = sg_proc_open_debug,
22881da177e4SLinus Torvalds 	.release = seq_release,
22891da177e4SLinus Torvalds };
22901da177e4SLinus Torvalds static struct seq_operations debug_seq_ops = {
22911da177e4SLinus Torvalds 	.start = dev_seq_start,
22921da177e4SLinus Torvalds 	.next  = dev_seq_next,
22931da177e4SLinus Torvalds 	.stop  = dev_seq_stop,
22941da177e4SLinus Torvalds 	.show  = sg_proc_seq_show_debug,
22951da177e4SLinus Torvalds };
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds struct sg_proc_leaf {
22991da177e4SLinus Torvalds 	const char * name;
23001da177e4SLinus Torvalds 	struct file_operations * fops;
23011da177e4SLinus Torvalds };
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds static struct sg_proc_leaf sg_proc_leaf_arr[] = {
23041da177e4SLinus Torvalds 	{"allow_dio", &adio_fops},
23051da177e4SLinus Torvalds 	{"debug", &debug_fops},
23061da177e4SLinus Torvalds 	{"def_reserved_size", &dressz_fops},
23071da177e4SLinus Torvalds 	{"device_hdr", &devhdr_fops},
23081da177e4SLinus Torvalds 	{"devices", &dev_fops},
23091da177e4SLinus Torvalds 	{"device_strs", &devstrs_fops},
23101da177e4SLinus Torvalds 	{"version", &version_fops}
23111da177e4SLinus Torvalds };
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds static int
23141da177e4SLinus Torvalds sg_proc_init(void)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	int k, mask;
23176391a113STobias Klauser 	int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
23181da177e4SLinus Torvalds 	struct sg_proc_leaf * leaf;
23191da177e4SLinus Torvalds 
232066600221SAl Viro 	sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
23211da177e4SLinus Torvalds 	if (!sg_proc_sgp)
23221da177e4SLinus Torvalds 		return 1;
23231da177e4SLinus Torvalds 	for (k = 0; k < num_leaves; ++k) {
23241da177e4SLinus Torvalds 		leaf = &sg_proc_leaf_arr[k];
23251da177e4SLinus Torvalds 		mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
2326a973909fSDenis V. Lunev 		leaf->fops->owner = THIS_MODULE;
2327a973909fSDenis V. Lunev 		leaf->fops->read = seq_read;
2328a973909fSDenis V. Lunev 		leaf->fops->llseek = seq_lseek;
2329a973909fSDenis V. Lunev 		proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
23301da177e4SLinus Torvalds 	}
23311da177e4SLinus Torvalds 	return 0;
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds static void
23351da177e4SLinus Torvalds sg_proc_cleanup(void)
23361da177e4SLinus Torvalds {
23371da177e4SLinus Torvalds 	int k;
23386391a113STobias Klauser 	int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	if (!sg_proc_sgp)
23411da177e4SLinus Torvalds 		return;
23421da177e4SLinus Torvalds 	for (k = 0; k < num_leaves; ++k)
23431da177e4SLinus Torvalds 		remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
23441da177e4SLinus Torvalds 	remove_proc_entry(sg_proc_sg_dirname, NULL);
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds static int sg_proc_seq_show_int(struct seq_file *s, void *v)
23491da177e4SLinus Torvalds {
23501da177e4SLinus Torvalds 	seq_printf(s, "%d\n", *((int *)s->private));
23511da177e4SLinus Torvalds 	return 0;
23521da177e4SLinus Torvalds }
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
23551da177e4SLinus Torvalds {
23561da177e4SLinus Torvalds 	return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds static ssize_t
23601da177e4SLinus Torvalds sg_proc_write_adio(struct file *filp, const char __user *buffer,
23611da177e4SLinus Torvalds 		   size_t count, loff_t *off)
23621da177e4SLinus Torvalds {
23631da177e4SLinus Torvalds 	int num;
23641da177e4SLinus Torvalds 	char buff[11];
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
23671da177e4SLinus Torvalds 		return -EACCES;
23681da177e4SLinus Torvalds 	num = (count < 10) ? count : 10;
23691da177e4SLinus Torvalds 	if (copy_from_user(buff, buffer, num))
23701da177e4SLinus Torvalds 		return -EFAULT;
23711da177e4SLinus Torvalds 	buff[num] = '\0';
23721da177e4SLinus Torvalds 	sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
23731da177e4SLinus Torvalds 	return count;
23741da177e4SLinus Torvalds }
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
23791da177e4SLinus Torvalds }
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds static ssize_t
23821da177e4SLinus Torvalds sg_proc_write_dressz(struct file *filp, const char __user *buffer,
23831da177e4SLinus Torvalds 		     size_t count, loff_t *off)
23841da177e4SLinus Torvalds {
23851da177e4SLinus Torvalds 	int num;
23861da177e4SLinus Torvalds 	unsigned long k = ULONG_MAX;
23871da177e4SLinus Torvalds 	char buff[11];
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
23901da177e4SLinus Torvalds 		return -EACCES;
23911da177e4SLinus Torvalds 	num = (count < 10) ? count : 10;
23921da177e4SLinus Torvalds 	if (copy_from_user(buff, buffer, num))
23931da177e4SLinus Torvalds 		return -EFAULT;
23941da177e4SLinus Torvalds 	buff[num] = '\0';
23951da177e4SLinus Torvalds 	k = simple_strtoul(buff, NULL, 10);
23961da177e4SLinus Torvalds 	if (k <= 1048576) {	/* limit "big buff" to 1 MB */
23971da177e4SLinus Torvalds 		sg_big_buff = k;
23981da177e4SLinus Torvalds 		return count;
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 	return -ERANGE;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds static int sg_proc_seq_show_version(struct seq_file *s, void *v)
24041da177e4SLinus Torvalds {
24051da177e4SLinus Torvalds 	seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
24061da177e4SLinus Torvalds 		   sg_version_date);
24071da177e4SLinus Torvalds 	return 0;
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds static int sg_proc_single_open_version(struct inode *inode, struct file *file)
24111da177e4SLinus Torvalds {
24121da177e4SLinus Torvalds 	return single_open(file, sg_proc_seq_show_version, NULL);
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
24161da177e4SLinus Torvalds {
24171da177e4SLinus Torvalds 	seq_printf(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\t"
24181da177e4SLinus Torvalds 		   "online\n");
24191da177e4SLinus Torvalds 	return 0;
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	return single_open(file, sg_proc_seq_show_devhdr, NULL);
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds struct sg_proc_deviter {
24281da177e4SLinus Torvalds 	loff_t	index;
24291da177e4SLinus Torvalds 	size_t	max;
24301da177e4SLinus Torvalds };
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds static void * dev_seq_start(struct seq_file *s, loff_t *pos)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
24351da177e4SLinus Torvalds 
2436729d70f5SJan Blunck 	s->private = it;
24371da177e4SLinus Torvalds 	if (! it)
24381da177e4SLinus Torvalds 		return NULL;
2439729d70f5SJan Blunck 
24401da177e4SLinus Torvalds 	it->index = *pos;
24411da177e4SLinus Torvalds 	it->max = sg_last_dev();
24421da177e4SLinus Torvalds 	if (it->index >= it->max)
24431da177e4SLinus Torvalds 		return NULL;
2444729d70f5SJan Blunck 	return it;
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
24481da177e4SLinus Torvalds {
2449729d70f5SJan Blunck 	struct sg_proc_deviter * it = s->private;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	*pos = ++it->index;
24521da177e4SLinus Torvalds 	return (it->index < it->max) ? it : NULL;
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds static void dev_seq_stop(struct seq_file *s, void *v)
24561da177e4SLinus Torvalds {
2457729d70f5SJan Blunck 	kfree(s->private);
24581da177e4SLinus Torvalds }
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds static int sg_proc_open_dev(struct inode *inode, struct file *file)
24611da177e4SLinus Torvalds {
24621da177e4SLinus Torvalds         return seq_open(file, &dev_seq_ops);
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
24681da177e4SLinus Torvalds 	Sg_device *sdp;
24691da177e4SLinus Torvalds 	struct scsi_device *scsidp;
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	sdp = it ? sg_get_dev(it->index) : NULL;
24721da177e4SLinus Torvalds 	if (sdp && (scsidp = sdp->device) && (!sdp->detached))
24731da177e4SLinus Torvalds 		seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
24741da177e4SLinus Torvalds 			      scsidp->host->host_no, scsidp->channel,
24751da177e4SLinus Torvalds 			      scsidp->id, scsidp->lun, (int) scsidp->type,
24761da177e4SLinus Torvalds 			      1,
24771da177e4SLinus Torvalds 			      (int) scsidp->queue_depth,
24781da177e4SLinus Torvalds 			      (int) scsidp->device_busy,
24791da177e4SLinus Torvalds 			      (int) scsi_device_online(scsidp));
24801da177e4SLinus Torvalds 	else
24811da177e4SLinus Torvalds 		seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
24821da177e4SLinus Torvalds 	return 0;
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
24861da177e4SLinus Torvalds {
24871da177e4SLinus Torvalds         return seq_open(file, &devstrs_seq_ops);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
24911da177e4SLinus Torvalds {
24921da177e4SLinus Torvalds 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
24931da177e4SLinus Torvalds 	Sg_device *sdp;
24941da177e4SLinus Torvalds 	struct scsi_device *scsidp;
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	sdp = it ? sg_get_dev(it->index) : NULL;
24971da177e4SLinus Torvalds 	if (sdp && (scsidp = sdp->device) && (!sdp->detached))
24981da177e4SLinus Torvalds 		seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
24991da177e4SLinus Torvalds 			   scsidp->vendor, scsidp->model, scsidp->rev);
25001da177e4SLinus Torvalds 	else
25011da177e4SLinus Torvalds 		seq_printf(s, "<no active device>\n");
25021da177e4SLinus Torvalds 	return 0;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
25061da177e4SLinus Torvalds {
25071da177e4SLinus Torvalds 	int k, m, new_interface, blen, usg;
25081da177e4SLinus Torvalds 	Sg_request *srp;
25091da177e4SLinus Torvalds 	Sg_fd *fp;
25101da177e4SLinus Torvalds 	const sg_io_hdr_t *hp;
25111da177e4SLinus Torvalds 	const char * cp;
2512cb59e840S 	unsigned int ms;
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 	for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
25151da177e4SLinus Torvalds 		seq_printf(s, "   FD(%d): timeout=%dms bufflen=%d "
25161da177e4SLinus Torvalds 			   "(res)sgat=%d low_dma=%d\n", k + 1,
25171da177e4SLinus Torvalds 			   jiffies_to_msecs(fp->timeout),
25181da177e4SLinus Torvalds 			   fp->reserve.bufflen,
25191da177e4SLinus Torvalds 			   (int) fp->reserve.k_use_sg,
25201da177e4SLinus Torvalds 			   (int) fp->low_dma);
25211da177e4SLinus Torvalds 		seq_printf(s, "   cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
25221da177e4SLinus Torvalds 			   (int) fp->cmd_q, (int) fp->force_packid,
25231da177e4SLinus Torvalds 			   (int) fp->keep_orphan, (int) fp->closed);
25241da177e4SLinus Torvalds 		for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) {
25251da177e4SLinus Torvalds 			hp = &srp->header;
25261da177e4SLinus Torvalds 			new_interface = (hp->interface_id == '\0') ? 0 : 1;
25271da177e4SLinus Torvalds 			if (srp->res_used) {
25281da177e4SLinus Torvalds 				if (new_interface &&
25291da177e4SLinus Torvalds 				    (SG_FLAG_MMAP_IO & hp->flags))
25301da177e4SLinus Torvalds 					cp = "     mmap>> ";
25311da177e4SLinus Torvalds 				else
25321da177e4SLinus Torvalds 					cp = "     rb>> ";
25331da177e4SLinus Torvalds 			} else {
25341da177e4SLinus Torvalds 				if (SG_INFO_DIRECT_IO_MASK & hp->info)
25351da177e4SLinus Torvalds 					cp = "     dio>> ";
25361da177e4SLinus Torvalds 				else
25371da177e4SLinus Torvalds 					cp = "     ";
25381da177e4SLinus Torvalds 			}
25391da177e4SLinus Torvalds 			seq_printf(s, cp);
2540d6b10348SMike Christie 			blen = srp->data.bufflen;
2541d6b10348SMike Christie 			usg = srp->data.k_use_sg;
25421da177e4SLinus Torvalds 			seq_printf(s, srp->done ?
25431da177e4SLinus Torvalds 				   ((1 == srp->done) ?  "rcv:" : "fin:")
2544d6b10348SMike Christie 				   : "act:");
25451da177e4SLinus Torvalds 			seq_printf(s, " id=%d blen=%d",
25461da177e4SLinus Torvalds 				   srp->header.pack_id, blen);
25471da177e4SLinus Torvalds 			if (srp->done)
25481da177e4SLinus Torvalds 				seq_printf(s, " dur=%d", hp->duration);
2549cb59e840S 			else {
2550cb59e840S 				ms = jiffies_to_msecs(jiffies);
25511da177e4SLinus Torvalds 				seq_printf(s, " t_o/elap=%d/%d",
2552cb59e840S 					(new_interface ? hp->timeout :
2553cb59e840S 						  jiffies_to_msecs(fp->timeout)),
2554cb59e840S 					(ms > hp->duration ? ms - hp->duration : 0));
2555cb59e840S 			}
25561da177e4SLinus Torvalds 			seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
25571da177e4SLinus Torvalds 				   (int) srp->data.cmd_opcode);
25581da177e4SLinus Torvalds 		}
25591da177e4SLinus Torvalds 		if (0 == m)
25601da177e4SLinus Torvalds 			seq_printf(s, "     No requests active\n");
25611da177e4SLinus Torvalds 	}
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds static int sg_proc_open_debug(struct inode *inode, struct file *file)
25651da177e4SLinus Torvalds {
25661da177e4SLinus Torvalds         return seq_open(file, &debug_seq_ops);
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
25701da177e4SLinus Torvalds {
25711da177e4SLinus Torvalds 	struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
25721da177e4SLinus Torvalds 	Sg_device *sdp;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	if (it && (0 == it->index)) {
25757c07d613SJames Bottomley 		seq_printf(s, "max_active_device=%d(origin 1)\n",
25767c07d613SJames Bottomley 			   (int)it->max);
25771da177e4SLinus Torvalds 		seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
25781da177e4SLinus Torvalds 	}
25791da177e4SLinus Torvalds 	sdp = it ? sg_get_dev(it->index) : NULL;
25801da177e4SLinus Torvalds 	if (sdp) {
25811da177e4SLinus Torvalds 		struct scsi_device *scsidp = sdp->device;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 		if (NULL == scsidp) {
25841da177e4SLinus Torvalds 			seq_printf(s, "device %d detached ??\n",
25851da177e4SLinus Torvalds 				   (int)it->index);
25861da177e4SLinus Torvalds 			return 0;
25871da177e4SLinus Torvalds 		}
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 		if (sg_get_nth_sfp(sdp, 0)) {
25901da177e4SLinus Torvalds 			seq_printf(s, " >>> device=%s ",
25911da177e4SLinus Torvalds 				sdp->disk->disk_name);
25921da177e4SLinus Torvalds 			if (sdp->detached)
25931da177e4SLinus Torvalds 				seq_printf(s, "detached pending close ");
25941da177e4SLinus Torvalds 			else
25951da177e4SLinus Torvalds 				seq_printf
25961da177e4SLinus Torvalds 				    (s, "scsi%d chan=%d id=%d lun=%d   em=%d",
25971da177e4SLinus Torvalds 				     scsidp->host->host_no,
25981da177e4SLinus Torvalds 				     scsidp->channel, scsidp->id,
25991da177e4SLinus Torvalds 				     scsidp->lun,
26001da177e4SLinus Torvalds 				     scsidp->host->hostt->emulated);
26011da177e4SLinus Torvalds 			seq_printf(s, " sg_tablesize=%d excl=%d\n",
26021da177e4SLinus Torvalds 				   sdp->sg_tablesize, sdp->exclude);
26031da177e4SLinus Torvalds 		}
26041da177e4SLinus Torvalds 		sg_proc_debug_helper(s, sdp);
26051da177e4SLinus Torvalds 	}
26061da177e4SLinus Torvalds 	return 0;
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds #endif				/* CONFIG_SCSI_PROC_FS */
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds module_init(init_sg);
26121da177e4SLinus Torvalds module_exit(exit_sg);
2613