* Original driver (sg.c):
* Copyright (C) 1992 Lawrence Foard
* Version 2 and 3 extensions to driver:
- * Copyright (C) 1998 - 2002 Douglas Gilbert
+ * Copyright (C) 1998 - 2005 Douglas Gilbert
*
* Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
*
* any later version.
*
*/
-#include <linux/config.h>
-static int sg_version_num = 30530; /* 2 digits for each component */
+
+static int sg_version_num = 30532; /* 2 digits for each component */
+#define SG_VERSION_STR "3.5.32"
+
/*
* D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au), notes:
* - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
* the kernel/module needs to be built with CONFIG_SCSI_LOGGING
* (otherwise the macros compile to empty statements).
- * Then before running the program to be debugged enter:
- * # echo "scsi log timeout 7" > /proc/scsi/scsi
- * This will send copious output to the console and the log which
- * is usually /var/log/messages. To turn off debugging enter:
- * # echo "scsi log timeout 0" > /proc/scsi/scsi
- * The 'timeout' token was chosen because it is relatively unused.
- * The token 'hlcomplete' should be used but that triggers too
- * much output from the sd device driver. To dump the current
- * state of the SCSI mid level data structures enter:
- * # echo "scsi dump 1" > /proc/scsi/scsi
- * To dump the state of sg's data structures use:
- * # cat /proc/scsi/sg/debug
*
*/
+#include <linux/config.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/poll.h>
-#include <linux/vmalloc.h>
#include <linux/smp_lock.h>
#include <linux/moduleparam.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/cdev.h>
#include <linux/seq_file.h>
-
#include <linux/blkdev.h>
+#include <linux/delay.h>
+
#include "scsi.h"
-#include "hosts.h"
+#include <scsi/scsi_host.h>
#include <scsi/scsi_driver.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>
#ifdef CONFIG_SCSI_PROC_FS
#include <linux/proc_fs.h>
-static char *sg_version_str = "3.5.30 [20040124]";
+static char *sg_version_date = "20050117";
static int sg_proc_init(void);
static void sg_proc_cleanup(void);
#define SG_ALLOW_DIO_DEF 0
#define SG_ALLOW_DIO_CODE /* compile out by commenting this define */
-#define SG_MAX_DEVS 8192
+#define SG_MAX_DEVS 32768
/*
* Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
#define SG_SECTOR_SZ 512
#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
-#define SG_DEV_ARR_LUMP 6 /* amount to over allocate sg_dev_arr by */
+#define SG_DEV_ARR_LUMP 32 /* amount to over allocate sg_dev_arr by */
static int sg_add(struct class_device *);
static void sg_remove(struct class_device *);
static Scsi_Request *dummy_cmdp; /* only used for sizeof */
-static rwlock_t sg_dev_arr_lock = RW_LOCK_UNLOCKED; /* Also used to lock
+static DEFINE_RWLOCK(sg_dev_arr_lock); /* Also used to lock
file descriptor list for device */
static struct class_interface sg_interface = {
static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
int tablesize);
-static ssize_t sg_new_read(Sg_fd * sfp, char *buf, size_t count,
+static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
Sg_request * srp);
-static ssize_t sg_new_write(Sg_fd * sfp, const char *buf, size_t count,
+static ssize_t sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
int blocking, int read_only, Sg_request ** o_srp);
static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
unsigned char *cmnd, int timeout, int blocking);
static int sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
- int wr_xf, int *countp, unsigned char **up);
+ int wr_xf, int *countp, unsigned char __user **up);
static int sg_write_xfer(Sg_request * srp);
static int sg_read_xfer(Sg_request * srp);
-static int sg_read_oxfer(Sg_request * srp, char *outp, int num_read_xfer);
+static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
static void sg_remove_scat(Sg_scatter_hold * schp);
static void sg_build_reserve(Sg_fd * sfp, int req_size);
static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
static Sg_request *sg_add_request(Sg_fd * sfp);
static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
static int sg_res_in_use(Sg_fd * sfp);
-static int sg_ms_to_jif(unsigned int msecs);
-static inline unsigned sg_jif_to_ms(int jifs);
static int sg_allow_access(unsigned char opcode, char dev_type);
static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
static Sg_device *sg_get_dev(int dev);
int res;
int retval;
+ nonseekable_open(inode, filp);
SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
sdp = sg_get_dev(dev);
if ((!sdp) || (!sdp->device))
}
static ssize_t
-sg_read(struct file *filp, char *buf, size_t count, loff_t * ppos)
+sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
{
int k, res;
Sg_device *sdp;
return -ENXIO;
SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
sdp->disk->disk_name, (int) count));
- if (ppos != &filp->f_pos) ; /* FIXME: Hmm. Seek to the right place, or fail? */
if ((k = verify_area(VERIFY_WRITE, buf, count)))
return k;
if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
}
static ssize_t
-sg_new_read(Sg_fd * sfp, char *buf, size_t count, Sg_request * srp)
+sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
{
sg_io_hdr_t *hp = &srp->header;
int err = 0;
}
static ssize_t
-sg_write(struct file *filp, const char *buf, size_t count, loff_t * ppos)
+sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
{
int mxsize, cmd_size, k;
int input_size, blocking;
if (!((filp->f_flags & O_NONBLOCK) ||
scsi_block_when_processing_errors(sdp->device)))
return -ENXIO;
- if (ppos != &filp->f_pos) ; /* FIXME: Hmm. Seek to the right place, or fail? */
if ((k = verify_area(VERIFY_READ, buf, count)))
return k; /* protects following copy_from_user()s + get_user()s */
else
hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
hp->dxfer_len = mxsize;
- hp->dxferp = (unsigned char *) buf + cmd_size;
+ hp->dxferp = (char __user *)buf + cmd_size;
hp->sbp = NULL;
hp->timeout = old_hdr.reply_len; /* structure abuse ... */
hp->flags = input_size; /* structure abuse ... */
hp->usr_ptr = NULL;
if (__copy_from_user(cmnd, buf, cmd_size))
return -EFAULT;
+ /*
+ * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
+ * but is is possible that the app intended SG_DXFER_TO_DEV, because there
+ * is a non-zero input_size, so emit a warning.
+ */
+ if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
+ if (printk_ratelimit())
+ printk(KERN_WARNING
+ "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
+ "guessing data in;\n" KERN_WARNING " "
+ "program %s not setting count and/or reply_len properly\n",
+ old_hdr.reply_len - (int)SZ_SG_HEADER,
+ input_size, (unsigned int) cmnd[0],
+ current->comm);
k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
return (k < 0) ? k : count;
}
static ssize_t
-sg_new_write(Sg_fd * sfp, const char *buf, size_t count,
+sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
int blocking, int read_only, Sg_request ** o_srp)
{
int k;
sg_io_hdr_t *hp;
unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
int timeout;
+ unsigned long ul_timeout;
if (count < SZ_SG_IO_HDR)
return -EINVAL;
return -EBUSY; /* reserve buffer already being used */
}
}
- timeout = sg_ms_to_jif(srp->header.timeout);
+ ul_timeout = msecs_to_jiffies(srp->header.timeout);
+ timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
sg_remove_request(sfp, srp);
return -EMSGSIZE;
(void *) SRpnt->sr_buffer, hp->dxfer_len,
sg_cmd_done, timeout, SG_DEFAULT_RETRIES);
/* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
- generic_unplug_device(q);
return 0;
}
+static int
+sg_srp_done(Sg_request *srp, Sg_fd *sfp)
+{
+ unsigned long iflags;
+ int done;
+
+ read_lock_irqsave(&sfp->rq_list_lock, iflags);
+ done = srp->done;
+ read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
+ return done;
+}
+
static int
sg_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd_in, unsigned long arg)
{
+ void __user *p = (void __user *)arg;
+ int __user *ip = p;
int result, val, read_only;
Sg_device *sdp;
Sg_fd *sfp;
return -ENODEV;
if (!scsi_block_when_processing_errors(sdp->device))
return -ENXIO;
- result = verify_area(VERIFY_WRITE, (void *) arg,
- SZ_SG_IO_HDR);
+ result = verify_area(VERIFY_WRITE, p, SZ_SG_IO_HDR);
if (result)
return result;
result =
- sg_new_write(sfp, (const char *) arg, SZ_SG_IO_HDR,
+ sg_new_write(sfp, p, SZ_SG_IO_HDR,
blocking, read_only, &srp);
if (result < 0)
return result;
while (1) {
result = 0; /* following macro to beat race condition */
__wait_event_interruptible(sfp->read_wait,
- (sdp->detached || sfp->closed || srp->done),
+ (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
result);
if (sdp->detached)
return -ENODEV;
srp->orphan = 1;
return result; /* -ERESTARTSYS because signal hit process */
}
+ write_lock_irqsave(&sfp->rq_list_lock, iflags);
srp->done = 2;
- result = sg_new_read(sfp, (char *) arg, SZ_SG_IO_HDR, srp);
+ write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
+ result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
return (result < 0) ? result : 0;
}
case SG_SET_TIMEOUT:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
if (val < 0)
/* strange ..., for backward compatibility */
return sfp->timeout_user;
case SG_SET_FORCE_LOW_DMA:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
if (val) {
}
return 0;
case SG_GET_LOW_DMA:
- return put_user((int) sfp->low_dma, (int *) arg);
+ return put_user((int) sfp->low_dma, ip);
case SG_GET_SCSI_ID:
result =
- verify_area(VERIFY_WRITE, (void *) arg,
- sizeof (sg_scsi_id_t));
+ verify_area(VERIFY_WRITE, p, sizeof (sg_scsi_id_t));
if (result)
return result;
else {
- sg_scsi_id_t *sg_idp = (sg_scsi_id_t *) arg;
+ sg_scsi_id_t __user *sg_idp = p;
if (sdp->detached)
return -ENODEV;
return 0;
}
case SG_SET_FORCE_PACK_ID:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
sfp->force_packid = val ? 1 : 0;
return 0;
case SG_GET_PACK_ID:
- result = verify_area(VERIFY_WRITE, (void *) arg, sizeof (int));
+ result = verify_area(VERIFY_WRITE, ip, sizeof (int));
if (result)
return result;
read_lock_irqsave(&sfp->rq_list_lock, iflags);
if ((1 == srp->done) && (!srp->sg_io_owned)) {
read_unlock_irqrestore(&sfp->rq_list_lock,
iflags);
- __put_user(srp->header.pack_id, (int *) arg);
+ __put_user(srp->header.pack_id, ip);
return 0;
}
}
read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
- __put_user(-1, (int *) arg);
+ __put_user(-1, ip);
return 0;
case SG_GET_NUM_WAITING:
read_lock_irqsave(&sfp->rq_list_lock, iflags);
++val;
}
read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
- return put_user(val, (int *) arg);
+ return put_user(val, ip);
case SG_GET_SG_TABLESIZE:
- return put_user(sdp->sg_tablesize, (int *) arg);
+ return put_user(sdp->sg_tablesize, ip);
case SG_SET_RESERVED_SIZE:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
if (val < 0)
return 0;
case SG_GET_RESERVED_SIZE:
val = (int) sfp->reserve.bufflen;
- return put_user(val, (int *) arg);
+ return put_user(val, ip);
case SG_SET_COMMAND_Q:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
sfp->cmd_q = val ? 1 : 0;
return 0;
case SG_GET_COMMAND_Q:
- return put_user((int) sfp->cmd_q, (int *) arg);
+ return put_user((int) sfp->cmd_q, ip);
case SG_SET_KEEP_ORPHAN:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
sfp->keep_orphan = val;
return 0;
case SG_GET_KEEP_ORPHAN:
- return put_user((int) sfp->keep_orphan, (int *) arg);
+ return put_user((int) sfp->keep_orphan, ip);
case SG_NEXT_CMD_LEN:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
sfp->next_cmd_len = (val > 0) ? val : 0;
return 0;
case SG_GET_VERSION_NUM:
- return put_user(sg_version_num, (int *) arg);
+ return put_user(sg_version_num, ip);
case SG_GET_ACCESS_COUNT:
/* faked - we don't have a real access count anymore */
val = (sdp->device ? 1 : 0);
- return put_user(val, (int *) arg);
+ return put_user(val, ip);
case SG_GET_REQUEST_TABLE:
- result = verify_area(VERIFY_WRITE, (void *) arg,
+ result = verify_area(VERIFY_WRITE, p,
SZ_SG_REQ_INFO * SG_MAX_QUEUE);
if (result)
return result;
srp->header.driver_status;
rinfo[val].duration =
srp->done ? srp->header.duration :
- sg_jif_to_ms(
+ jiffies_to_msecs(
jiffies - srp->header.duration);
rinfo[val].orphan = srp->orphan;
rinfo[val].sg_io_owned = srp->sg_io_owned;
}
}
read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
- return (__copy_to_user((void *) arg, rinfo,
+ return (__copy_to_user(p, rinfo,
SZ_SG_REQ_INFO * SG_MAX_QUEUE) ? -EFAULT : 0);
}
case SG_EMULATED_HOST:
if (sdp->detached)
return -ENODEV;
- return put_user(sdp->device->host->hostt->emulated, (int *) arg);
+ return put_user(sdp->device->host->hostt->emulated, ip);
case SG_SCSI_RESET:
if (sdp->detached)
return -ENODEV;
return -EBUSY;
} else if (!scsi_block_when_processing_errors(sdp->device))
return -EBUSY;
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
if (SG_SCSI_RESET_NOTHING == val)
return -ENODEV;
if (read_only) {
unsigned char opcode = WRITE_6;
- Scsi_Ioctl_Command *siocp = (void *) arg;
+ Scsi_Ioctl_Command __user *siocp = p;
if (copy_from_user(&opcode, siocp->data, 1))
return -EFAULT;
if (!sg_allow_access(opcode, sdp->device->type))
return -EPERM;
}
- return scsi_ioctl_send_command(sdp->device, (void *) arg);
+ return scsi_ioctl_send_command(sdp->device, p);
case SG_SET_DEBUG:
- result = get_user(val, (int *) arg);
+ result = get_user(val, ip);
if (result)
return result;
sdp->sgdebug = (char) val;
case SG_GET_TRANSFORM:
if (sdp->detached)
return -ENODEV;
- return scsi_ioctl(sdp->device, cmd_in, (void *) arg);
+ return scsi_ioctl(sdp->device, cmd_in, p);
default:
if (read_only)
return -EPERM; /* don't know so take safe approach */
- return scsi_ioctl(sdp->device, cmd_in, (void *) arg);
+ return scsi_ioctl(sdp->device, cmd_in, p);
}
}
page_ptr = sg_scatg2virt(sclp) + m;
page = virt_to_page(page_ptr);
if (startFinish)
- atomic_inc(&page->count);
+ get_page(page);
else {
if (page_count(page) > 0)
- atomic_dec(&page->count);
+ __put_page(page);
}
}
}
page_ptr = (unsigned char *) rsv_schp->buffer + m;
page = virt_to_page(page_ptr);
if (startFinish)
- atomic_inc(&page->count);
+ get_page(page);
else {
if (page_count(page) > 0)
- atomic_dec(&page->count);
+ __put_page(page);
}
}
}
Sg_device *sdp = NULL;
Sg_fd *sfp;
Sg_request *srp = NULL;
+ unsigned long iflags;
if (SCpnt && (SRpnt = SCpnt->sc_request))
srp = (Sg_request *) SRpnt->upper_private_data;
srp->header.resid = SCpnt->resid;
/* N.B. unit of duration changes here from jiffies to millisecs */
srp->header.duration =
- sg_jif_to_ms(jiffies - (int) srp->header.duration);
+ jiffies_to_msecs(jiffies - srp->header.duration);
if (0 != SRpnt->sr_result) {
+ struct scsi_sense_hdr sshdr;
+
memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
sizeof (srp->sense_b));
srp->header.status = 0xff & SRpnt->sr_result;
/* Following if statement is a patch supplied by Eric Youngdale */
if (driver_byte(SRpnt->sr_result) != 0
- && (SRpnt->sr_sense_buffer[0] & 0x7f) == 0x70
- && (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION
+ && scsi_command_normalize_sense(SCpnt, &sshdr)
+ && !scsi_sense_is_deferred(&sshdr)
+ && sshdr.sense_key == UNIT_ATTENTION
&& sdp->device->removable) {
- /* Detected disc change. Set the bit - this may be used if */
- /* there are filesystems using this device. */
+ /* Detected possible disc change. Set the bit - this */
+ /* may be used if there are filesystems using this device */
sdp->device->changed = 1;
}
}
if (sfp && srp) {
/* Now wake up any sg_read() that is waiting for this packet. */
kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
+ write_lock_irqsave(&sfp->rq_list_lock, iflags);
srp->done = 1;
wake_up_interruptible(&sfp->read_wait);
+ write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
}
}
static int sg_sysfs_valid = 0;
-static int
-sg_add(struct class_device *cl_dev)
+static int sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
{
- struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
- struct gendisk *disk;
- Sg_device *sdp = NULL;
+ Sg_device *sdp;
unsigned long iflags;
- struct cdev * cdev = NULL;
+ void *old_sg_dev_arr = NULL;
int k, error;
- disk = alloc_disk(1);
- if (!disk)
+ sdp = kmalloc(sizeof(Sg_device), GFP_KERNEL);
+ if (!sdp) {
+ printk(KERN_WARNING "kmalloc Sg_device failure\n");
return -ENOMEM;
+ }
- cdev = cdev_alloc();
- if (! cdev)
- return -ENOMEM;
write_lock_irqsave(&sg_dev_arr_lock, iflags);
- if (sg_nr_dev >= sg_dev_max) { /* try to resize */
+ if (unlikely(sg_nr_dev >= sg_dev_max)) { /* try to resize */
Sg_device **tmp_da;
int tmp_dev_max = sg_nr_dev + SG_DEV_ARR_LUMP;
-
write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
- tmp_da = (Sg_device **)vmalloc(
- tmp_dev_max * sizeof(Sg_device *));
- if (NULL == tmp_da) {
- printk(KERN_ERR
- "sg_add: device array cannot be resized\n");
- error = -ENOMEM;
- goto out;
- }
+
+ tmp_da = kmalloc(tmp_dev_max * sizeof(Sg_device *), GFP_KERNEL);
+ if (unlikely(!tmp_da))
+ goto expand_failed;
+
write_lock_irqsave(&sg_dev_arr_lock, iflags);
- memset(tmp_da, 0, tmp_dev_max * sizeof (Sg_device *));
- memcpy(tmp_da, sg_dev_arr,
- sg_dev_max * sizeof (Sg_device *));
- vfree((char *) sg_dev_arr);
+ memset(tmp_da, 0, tmp_dev_max * sizeof(Sg_device *));
+ memcpy(tmp_da, sg_dev_arr, sg_dev_max * sizeof(Sg_device *));
+ old_sg_dev_arr = sg_dev_arr;
sg_dev_arr = tmp_da;
sg_dev_max = tmp_dev_max;
}
-find_empty_slot:
for (k = 0; k < sg_dev_max; k++)
if (!sg_dev_arr[k])
break;
- if (k >= SG_MAX_DEVS) {
- write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
- printk(KERN_WARNING
- "Unable to attach sg device <%d, %d, %d, %d>"
- " type=%d, minor number exceeds %d\n",
- scsidp->host->host_no, scsidp->channel, scsidp->id,
- scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
- if (NULL != sdp)
- vfree((char *) sdp);
- error = -ENODEV;
- goto out;
- }
- if (k < sg_dev_max) {
- if (NULL == sdp) {
- write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
- sdp = (Sg_device *)vmalloc(sizeof(Sg_device));
- write_lock_irqsave(&sg_dev_arr_lock, iflags);
- if (!sg_dev_arr[k])
- goto find_empty_slot;
- }
- } else
- sdp = NULL;
- if (NULL == sdp) {
- write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
- printk(KERN_ERR "sg_add: Sg_device cannot be allocated\n");
- error = -ENOMEM;
- goto out;
- }
+ if (unlikely(k >= SG_MAX_DEVS))
+ goto overflow;
- SCSI_LOG_TIMEOUT(3, printk("sg_add: dev=%d \n", k));
memset(sdp, 0, sizeof(*sdp));
+ SCSI_LOG_TIMEOUT(3, printk("sg_alloc: dev=%d \n", k));
sprintf(disk->disk_name, "sg%d", k);
- cdev->owner = THIS_MODULE;
- cdev->ops = &sg_fops;
- disk->major = SCSI_GENERIC_MAJOR;
disk->first_minor = k;
sdp->disk = disk;
sdp->device = scsidp;
sg_nr_dev++;
sg_dev_arr[k] = sdp;
write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
+ error = k;
+
+ out:
+ if (error < 0)
+ kfree(sdp);
+ kfree(old_sg_dev_arr);
+ return error;
+
+ expand_failed:
+ printk(KERN_WARNING "sg_alloc: device array cannot be resized\n");
+ error = -ENOMEM;
+ goto out;
+
+ overflow:
+ write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
+ printk(KERN_WARNING
+ "Unable to attach sg device <%d, %d, %d, %d> type=%d, minor "
+ "number exceeds %d\n", scsidp->host->host_no, scsidp->channel,
+ scsidp->id, scsidp->lun, scsidp->type, SG_MAX_DEVS - 1);
+ error = -ENODEV;
+ goto out;
+}
+
+static int
+sg_add(struct class_device *cl_dev)
+{
+ struct scsi_device *scsidp = to_scsi_device(cl_dev->dev);
+ struct gendisk *disk;
+ Sg_device *sdp = NULL;
+ struct cdev * cdev = NULL;
+ int error, k;
+
+ disk = alloc_disk(1);
+ if (!disk) {
+ printk(KERN_WARNING "alloc_disk failed\n");
+ return -ENOMEM;
+ }
+ disk->major = SCSI_GENERIC_MAJOR;
+
+ error = -ENOMEM;
+ cdev = cdev_alloc();
+ if (!cdev) {
+ printk(KERN_WARNING "cdev_alloc failed\n");
+ goto out;
+ }
+ cdev->owner = THIS_MODULE;
+ cdev->ops = &sg_fops;
+
+ error = sg_alloc(disk, scsidp);
+ if (error < 0) {
+ printk(KERN_WARNING "sg_alloc failed\n");
+ goto out;
+ }
+ k = error;
+ sdp = sg_dev_arr[k];
devfs_mk_cdev(MKDEV(SCSI_GENERIC_MAJOR, k),
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
tsfp = sfp->nextfp;
for (srp = sfp->headrp; srp; srp = tsrp) {
tsrp = srp->nextrp;
- if (sfp->closed || (0 == srp->done))
+ if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
sg_finish_rem_req(srp);
}
if (sfp->closed) {
put_disk(sdp->disk);
sdp->disk = NULL;
if (NULL == sdp->headfp)
- vfree((char *) sdp);
+ kfree((char *) sdp);
}
if (delay)
- scsi_sleep(2); /* dirty detach so delay device destruction */
+ msleep(10); /* dirty detach so delay device destruction */
}
/* Set 'perm' (4th argument) to 0 to disable module_param's definition
* of sysfs parameters (which module_param doesn't yet support).
* Sysfs parameters defined explicitly below.
*/
-module_param_named(def_reserved_size, def_reserved_size, int, 0);
-module_param_named(allow_dio, sg_allow_dio, int, 0);
+module_param_named(def_reserved_size, def_reserved_size, int, S_IRUGO);
+module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
MODULE_AUTHOR("Douglas Gilbert");
MODULE_DESCRIPTION("SCSI generic (sg) driver");
MODULE_LICENSE("GPL");
+MODULE_VERSION(SG_VERSION_STR);
MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
SG_MAX_DEVS);
if (sg_dev_arr != NULL) {
- vfree((char *) sg_dev_arr);
+ kfree((char *) sg_dev_arr);
sg_dev_arr = NULL;
}
sg_dev_max = 0;
int iovec_count = (int) hp->iovec_count;
int dxfer_dir = hp->dxfer_direction;
unsigned char *p;
- unsigned char *up;
+ unsigned char __user *up;
int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_TO_DEV == dxfer_dir) ||
static int
sg_u_iovec(sg_io_hdr_t * hp, int sg_num, int ind,
- int wr_xf, int *countp, unsigned char **up)
+ int wr_xf, int *countp, unsigned char __user **up)
{
int num_xfer = (int) hp->dxfer_len;
- unsigned char *p;
+ unsigned char __user *p = hp->dxferp;
int count, k;
- sg_iovec_t u_iovec;
if (0 == sg_num) {
- p = (unsigned char *) hp->dxferp;
if (wr_xf && ('\0' == hp->interface_id))
count = (int) hp->flags; /* holds "old" input_size */
else
count = num_xfer;
} else {
- if (__copy_from_user(&u_iovec,
- (unsigned char *) hp->dxferp +
- (ind * SZ_SG_IOVEC), SZ_SG_IOVEC))
+ sg_iovec_t iovec;
+ if (__copy_from_user(&iovec, p + ind*SZ_SG_IOVEC, SZ_SG_IOVEC))
return -EFAULT;
- p = (unsigned char *) u_iovec.iov_base;
- count = (int) u_iovec.iov_len;
+ p = iovec.iov_base;
+ count = (int) iovec.iov_len;
}
if ((k = verify_area(wr_xf ? VERIFY_READ : VERIFY_WRITE, p, count)))
return k;
int iovec_count = (int) hp->iovec_count;
int dxfer_dir = hp->dxfer_direction;
unsigned char *p;
- unsigned char *up;
+ unsigned char __user *up;
int new_interface = ('\0' == hp->interface_id) ? 0 : 1;
if ((SG_DXFER_UNKNOWN == dxfer_dir) || (SG_DXFER_FROM_DEV == dxfer_dir)
}
static int
-sg_read_oxfer(Sg_request * srp, char *outp, int num_read_xfer)
+sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
{
Sg_scatter_hold *schp = &srp->data;
Sg_fd *sfp;
unsigned long iflags;
- sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, 0);
+ sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
if (!sfp)
return NULL;
memset(sfp, 0, sizeof (Sg_fd));
init_waitqueue_head(&sfp->read_wait);
- sfp->rq_list_lock = RW_LOCK_UNLOCKED;
+ rwlock_init(&sfp->rq_list_lock);
sfp->timeout = SG_DEFAULT_TIMEOUT;
sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
for (srp = sfp->headrp; srp; srp = tsrp) {
tsrp = srp->nextrp;
- if (srp->done)
+ if (sg_srp_done(srp, sfp))
sg_finish_rem_req(srp);
else
++dirty;
}
if (k < maxd)
sg_dev_arr[k] = NULL;
- vfree((char *) sdp);
+ kfree((char *) sdp);
res = 1;
}
write_unlock_irqrestore(&sg_dev_arr_lock, iflags);
free_pages((unsigned long) buff, order);
}
-static int
-sg_ms_to_jif(unsigned int msecs)
-{
- if ((UINT_MAX / 2U) < msecs)
- return INT_MAX; /* special case, set largest possible */
- else
- return ((int) msecs <
- (INT_MAX / 1000)) ? (((int) msecs * HZ) / 1000)
- : (((int) msecs / 1000) * HZ);
-}
-
-static inline unsigned
-sg_jif_to_ms(int jifs)
-{
- if (jifs <= 0)
- return 0U;
- else {
- unsigned int j = (unsigned int) jifs;
- return (j <
- (UINT_MAX / 1000)) ? ((j * 1000) / HZ) : ((j / HZ) *
- 1000);
- }
-}
+#ifndef MAINTENANCE_IN_CMD
+#define MAINTENANCE_IN_CMD 0xa3
+#endif
static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
- MODE_SENSE, MODE_SENSE_10, LOG_SENSE
+ READ_16, MODE_SENSE, MODE_SENSE_10, LOG_SENSE, REPORT_LUNS,
+ SERVICE_ACTION_IN, RECEIVE_DIAGNOSTIC, READ_LONG, MAINTENANCE_IN_CMD
};
static int
if (copy_from_user(buff, buffer, num))
return -EFAULT;
buff[num] = '\0';
- sg_allow_dio = simple_strtoul(buff, 0, 10) ? 1 : 0;
+ sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
return count;
}
if (copy_from_user(buff, buffer, num))
return -EFAULT;
buff[num] = '\0';
- k = simple_strtoul(buff, 0, 10);
+ k = simple_strtoul(buff, NULL, 10);
if (k <= 1048576) { /* limit "big buff" to 1 MB */
sg_big_buff = k;
return count;
static int sg_proc_seq_show_version(struct seq_file *s, void *v)
{
- seq_printf(s, "%d\t%s\n", sg_version_num, sg_version_str);
+ seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
+ sg_version_date);
return 0;
}
for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
"(res)sgat=%d low_dma=%d\n", k + 1,
- sg_jif_to_ms(fp->timeout),
+ jiffies_to_msecs(fp->timeout),
fp->reserve.bufflen,
(int) fp->reserve.k_use_sg,
(int) fp->low_dma);
seq_printf(s, " dur=%d", hp->duration);
else
seq_printf(s, " t_o/elap=%d/%d",
- new_interface ? hp->timeout : sg_jif_to_ms(fp->timeout),
- sg_jif_to_ms(hp->duration ? (jiffies - hp->duration) : 0));
+ new_interface ? hp->timeout : jiffies_to_msecs(fp->timeout),
+ jiffies_to_msecs(hp->duration ? (jiffies - hp->duration) : 0));
seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
(int) srp->data.cmd_opcode);
}