Files

309 lines
7.1 KiB
C
Raw Permalink 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 <linux/io_uring/cmd.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;
bsg_uring_cmd_fn *uring_cmd_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
2023-06-14 12:36:10 +02:00
#define BSG_MAX_DEVS (1 << MINORBITS)
2021-07-29 08:48:42 +02:00
static DEFINE_IDA(bsg_minor_ida);
static const 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);
}
static int bsg_sg_io(struct bsg_device *bd, bool open_for_write,
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, open_for_write,
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:
return bsg_sg_io(bd, file->f_mode & FMODE_WRITE, uarg);
case SCSI_IOCTL_SEND_COMMAND:
pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
current->comm);
return -EINVAL;
default:
return -ENOTTY;
}
}
static int bsg_check_uring_features(unsigned int issue_flags)
{
/* BSG passthrough requires big SQE/CQE support */
if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
(IO_URING_F_SQE128|IO_URING_F_CQE32))
return -EOPNOTSUPP;
return 0;
}
static int bsg_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
{
struct bsg_device *bd = to_bsg_device(file_inode(ioucmd->file));
bool open_for_write = ioucmd->file->f_mode & FMODE_WRITE;
struct request_queue *q = bd->queue;
int ret;
ret = bsg_check_uring_features(issue_flags);
if (ret)
return ret;
if (!bd->uring_cmd_fn)
return -EOPNOTSUPP;
return bd->uring_cmd_fn(q, ioucmd, issue_flags, open_for_write);
}
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,
.uring_cmd = bsg_uring_cmd,
.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_free(&bsg_minor_ida, MINOR(bd->device.devt));
2021-09-11 18:53:06 +08:00
kfree(bd);
}
2021-07-29 08:48:42 +02:00
void bsg_unregister_queue(struct bsg_device *bd)
{
struct gendisk *disk = bd->queue->disk;
if (disk && disk->queue_kobj.sd)
sysfs_remove_link(&disk->queue_kobj, "bsg");
2021-07-29 08:48:42 +02:00
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,
bsg_uring_cmd_fn *uring_cmd_fn)
{
2021-07-29 08:48:42 +02:00
struct bsg_device *bd;
2013-02-27 17:03:57 -08:00
int ret;
bd = kzalloc_obj(*bd);
2021-07-29 08:48:42 +02:00
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;
bd->uring_cmd_fn = uring_cmd_fn;
ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, 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;
2021-07-29 08:48:42 +02:00
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
if (q->disk && q->disk->queue_kobj.sd) {
ret = sysfs_create_link(&q->disk->queue_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(const struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
}
static const struct class bsg_class = {
.name = "bsg",
.devnode = bsg_devnode,
};
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;
ret = class_register(&bsg_class);
if (ret)
return ret;
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_unregister(&bsg_class);
2007-07-17 12:20:46 +02:00
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);