Files
laptops-kernel/block/bsg.c
T

268 lines
6.1 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0
/*
2007-07-29 23:00:46 +09:00
* bsg.c - block layer implementation of the sg v4 interface
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/file.h>
#include <linux/blkdev.h>
#include <linux/cdev.h>
2009-11-11 13:47:45 +01:00
#include <linux/jiffies.h>
#include <linux/percpu.h>
#include <linux/idr.h>
#include <linux/bsg.h>
#include <linux/slab.h>
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>
2007-07-17 12:21:35 +02:00
#define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
#define BSG_VERSION "0.4"
struct bsg_device {
2007-07-24 09:28:11 +02:00
struct request_queue *queue;
2021-07-29 08:48:42 +02:00
struct device device;
struct cdev cdev;
int max_queue;
unsigned int timeout;
unsigned int reserved_size;
bsg_sg_io_fn *sg_io_fn;
};
2021-07-29 08:48:42 +02:00
static inline struct bsg_device *to_bsg_device(struct inode *inode)
{
return container_of(inode->i_cdev, struct bsg_device, cdev);
}
2007-01-23 16:24:41 +01:00
#define BSG_DEFAULT_CMDS 64
2007-03-28 13:29:58 +02:00
#define BSG_MAX_DEVS 32768
2021-07-29 08:48:42 +02:00
static DEFINE_IDA(bsg_minor_ida);
static struct class *bsg_class;
2007-07-17 08:56:10 +02:00
static int bsg_major;
static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
{
unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
if (hdr->timeout)
timeout = msecs_to_jiffies(hdr->timeout);
else if (bd->timeout)
timeout = bd->timeout;
return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
}
2021-07-29 08:48:42 +02:00
static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg)
{
2018-11-09 20:12:25 +01:00
struct sg_io_v4 hdr;
int ret;
2018-11-09 20:12:25 +01:00
if (copy_from_user(&hdr, uarg, sizeof(hdr)))
return -EFAULT;
if (hdr.guard != 'Q')
return -EINVAL;
ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr));
if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
2018-11-09 20:12:25 +01:00
return -EFAULT;
return ret;
2006-12-20 11:20:15 +01:00
}
static int bsg_open(struct inode *inode, struct file *file)
{
2021-07-29 08:48:42 +02:00
if (!blk_get_queue(to_bsg_device(inode)->queue))
return -ENXIO;
return 0;
}
static int bsg_release(struct inode *inode, struct file *file)
{
2021-07-29 08:48:42 +02:00
blk_put_queue(to_bsg_device(inode)->queue);
return 0;
}
2018-11-09 20:12:25 +01:00
static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
{
2021-07-29 08:48:42 +02:00
return put_user(READ_ONCE(bd->max_queue), uarg);
2018-11-09 20:12:25 +01:00
}
static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
{
2021-07-29 08:48:42 +02:00
int max_queue;
2018-11-09 20:12:25 +01:00
2021-07-29 08:48:42 +02:00
if (get_user(max_queue, uarg))
2018-11-09 20:12:25 +01:00
return -EFAULT;
2021-07-29 08:48:42 +02:00
if (max_queue < 1)
2018-11-09 20:12:25 +01:00
return -EINVAL;
2021-07-29 08:48:42 +02:00
WRITE_ONCE(bd->max_queue, max_queue);
2018-11-09 20:12:25 +01:00
return 0;
}
2007-07-17 08:52:29 +02:00
static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
2021-07-29 08:48:42 +02:00
struct bsg_device *bd = to_bsg_device(file_inode(file));
2021-07-24 09:20:22 +02:00
struct request_queue *q = bd->queue;
2018-11-09 20:12:25 +01:00
void __user *uarg = (void __user *) arg;
2021-07-24 09:20:22 +02:00
int __user *intp = uarg;
int val;
switch (cmd) {
2018-11-09 20:12:25 +01:00
/*
* Our own ioctls
*/
case SG_GET_COMMAND_Q:
2018-11-09 20:12:25 +01:00
return bsg_get_command_q(bd, uarg);
case SG_SET_COMMAND_Q:
return bsg_set_command_q(bd, uarg);
/*
* SCSI/sg ioctls
*/
case SG_GET_VERSION_NUM:
2021-07-24 09:20:22 +02:00
return put_user(30527, intp);
case SCSI_IOCTL_GET_IDLUN:
2021-07-24 09:20:22 +02:00
return put_user(0, intp);
case SCSI_IOCTL_GET_BUS_NUMBER:
2021-07-24 09:20:22 +02:00
return put_user(0, intp);
case SG_SET_TIMEOUT:
2021-07-24 09:20:22 +02:00
if (get_user(val, intp))
return -EFAULT;
bd->timeout = clock_t_to_jiffies(val);
2021-07-24 09:20:22 +02:00
return 0;
case SG_GET_TIMEOUT:
return jiffies_to_clock_t(bd->timeout);
case SG_GET_RESERVED_SIZE:
return put_user(min(bd->reserved_size, queue_max_bytes(q)),
2021-07-24 09:20:22 +02:00
intp);
case SG_SET_RESERVED_SIZE:
2021-07-24 09:20:22 +02:00
if (get_user(val, intp))
return -EFAULT;
if (val < 0)
return -EINVAL;
bd->reserved_size =
2021-07-24 09:20:22 +02:00
min_t(unsigned int, val, queue_max_bytes(q));
return 0;
case SG_EMULATED_HOST:
2021-07-24 09:20:22 +02:00
return put_user(1, intp);
2018-11-09 20:12:25 +01:00
case SG_IO:
2021-07-29 08:48:42 +02:00
return bsg_sg_io(bd, file->f_mode, uarg);
case SCSI_IOCTL_SEND_COMMAND:
pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
current->comm);
return -EINVAL;
default:
return -ENOTTY;
}
}
2007-10-15 11:01:53 +02:00
static const struct file_operations bsg_fops = {
.open = bsg_open,
.release = bsg_release,
2007-07-17 08:52:29 +02:00
.unlocked_ioctl = bsg_ioctl,
2019-03-15 17:13:06 +01:00
.compat_ioctl = compat_ptr_ioctl,
.owner = THIS_MODULE,
2010-08-15 18:52:59 +02:00
.llseek = default_llseek,
};
2021-09-11 18:53:06 +08:00
static void bsg_device_release(struct device *dev)
{
struct bsg_device *bd = container_of(dev, struct bsg_device, device);
ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt));
kfree(bd);
}
2021-07-29 08:48:42 +02:00
void bsg_unregister_queue(struct bsg_device *bd)
{
2021-07-29 08:48:42 +02:00
if (bd->queue->kobj.sd)
sysfs_remove_link(&bd->queue->kobj, "bsg");
cdev_device_del(&bd->cdev, &bd->device);
2021-09-11 18:53:06 +08:00
put_device(&bd->device);
}
2007-03-30 11:19:39 +02:00
EXPORT_SYMBOL_GPL(bsg_unregister_queue);
2021-07-29 08:48:42 +02:00
struct bsg_device *bsg_register_queue(struct request_queue *q,
struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
{
2021-07-29 08:48:42 +02:00
struct bsg_device *bd;
2013-02-27 17:03:57 -08:00
int ret;
2021-07-29 08:48:42 +02:00
bd = kzalloc(sizeof(*bd), GFP_KERNEL);
if (!bd)
return ERR_PTR(-ENOMEM);
bd->max_queue = BSG_DEFAULT_CMDS;
bd->reserved_size = INT_MAX;
2021-07-29 08:48:42 +02:00
bd->queue = q;
bd->sg_io_fn = sg_io_fn;
2021-07-29 08:48:42 +02:00
ret = ida_simple_get(&bsg_minor_ida, 0, BSG_MAX_DEVS, GFP_KERNEL);
2013-02-27 17:03:57 -08:00
if (ret < 0) {
2021-07-29 08:48:42 +02:00
if (ret == -ENOSPC)
dev_err(parent, "bsg: too many bsg devices\n");
2021-09-11 18:53:06 +08:00
kfree(bd);
return ERR_PTR(ret);
}
2021-07-29 08:48:42 +02:00
bd->device.devt = MKDEV(bsg_major, ret);
bd->device.class = bsg_class;
bd->device.parent = parent;
2021-09-11 18:53:06 +08:00
bd->device.release = bsg_device_release;
2021-07-29 08:48:42 +02:00
dev_set_name(&bd->device, "%s", name);
device_initialize(&bd->device);
2021-07-29 08:48:42 +02:00
cdev_init(&bd->cdev, &bsg_fops);
bd->cdev.owner = THIS_MODULE;
ret = cdev_device_add(&bd->cdev, &bd->device);
if (ret)
2021-09-11 18:53:06 +08:00
goto out_put_device;
2007-03-28 13:29:24 +02:00
2007-07-16 11:18:23 -07:00
if (q->kobj.sd) {
2021-07-29 08:48:42 +02:00
ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg");
2007-03-28 13:29:24 +02:00
if (ret)
2021-07-29 08:48:42 +02:00
goto out_device_del;
2007-03-28 13:29:24 +02:00
}
2021-07-29 08:48:42 +02:00
return bd;
2021-07-29 08:48:42 +02:00
out_device_del:
cdev_device_del(&bd->cdev, &bd->device);
2021-09-11 18:53:06 +08:00
out_put_device:
put_device(&bd->device);
2021-07-29 08:48:42 +02:00
return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(bsg_register_queue);
static char *bsg_devnode(struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
}
static int __init bsg_init(void)
{
2007-07-17 08:56:10 +02:00
dev_t devid;
2021-07-29 08:48:42 +02:00
int ret;
bsg_class = class_create(THIS_MODULE, "bsg");
2018-07-12 10:09:59 +02:00
if (IS_ERR(bsg_class))
return PTR_ERR(bsg_class);
bsg_class->devnode = bsg_devnode;
2007-07-17 08:56:10 +02:00
ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
2007-07-17 12:20:46 +02:00
if (ret)
goto destroy_bsg_class;
2007-07-17 08:56:10 +02:00
bsg_major = MAJOR(devid);
2007-07-17 15:10:09 +02:00
printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
2007-07-17 12:21:35 +02:00
" loaded (major %d)\n", bsg_major);
return 0;
2021-07-29 08:48:42 +02:00
2007-07-17 12:20:46 +02:00
destroy_bsg_class:
class_destroy(bsg_class);
return ret;
}
MODULE_AUTHOR("Jens Axboe");
2007-07-17 12:21:35 +02:00
MODULE_DESCRIPTION(BSG_DESCRIPTION);
MODULE_LICENSE("GPL");
2007-03-28 13:29:24 +02:00
device_initcall(bsg_init);