Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * bsg.c - block layer implementation of the sg v4 interface
0004  */
0005 #include <linux/module.h>
0006 #include <linux/init.h>
0007 #include <linux/file.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/cdev.h>
0010 #include <linux/jiffies.h>
0011 #include <linux/percpu.h>
0012 #include <linux/idr.h>
0013 #include <linux/bsg.h>
0014 #include <linux/slab.h>
0015 
0016 #include <scsi/scsi.h>
0017 #include <scsi/scsi_ioctl.h>
0018 #include <scsi/sg.h>
0019 
0020 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
0021 #define BSG_VERSION "0.4"
0022 
0023 struct bsg_device {
0024     struct request_queue *queue;
0025     struct device device;
0026     struct cdev cdev;
0027     int max_queue;
0028     unsigned int timeout;
0029     unsigned int reserved_size;
0030     bsg_sg_io_fn *sg_io_fn;
0031 };
0032 
0033 static inline struct bsg_device *to_bsg_device(struct inode *inode)
0034 {
0035     return container_of(inode->i_cdev, struct bsg_device, cdev);
0036 }
0037 
0038 #define BSG_DEFAULT_CMDS    64
0039 #define BSG_MAX_DEVS        32768
0040 
0041 static DEFINE_IDA(bsg_minor_ida);
0042 static struct class *bsg_class;
0043 static int bsg_major;
0044 
0045 static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
0046 {
0047     unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
0048 
0049     if (hdr->timeout)
0050         timeout = msecs_to_jiffies(hdr->timeout);
0051     else if (bd->timeout)
0052         timeout = bd->timeout;
0053 
0054     return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
0055 }
0056 
0057 static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg)
0058 {
0059     struct sg_io_v4 hdr;
0060     int ret;
0061 
0062     if (copy_from_user(&hdr, uarg, sizeof(hdr)))
0063         return -EFAULT;
0064     if (hdr.guard != 'Q')
0065         return -EINVAL;
0066     ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr));
0067     if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
0068         return -EFAULT;
0069     return ret;
0070 }
0071 
0072 static int bsg_open(struct inode *inode, struct file *file)
0073 {
0074     if (!blk_get_queue(to_bsg_device(inode)->queue))
0075         return -ENXIO;
0076     return 0;
0077 }
0078 
0079 static int bsg_release(struct inode *inode, struct file *file)
0080 {
0081     blk_put_queue(to_bsg_device(inode)->queue);
0082     return 0;
0083 }
0084 
0085 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
0086 {
0087     return put_user(READ_ONCE(bd->max_queue), uarg);
0088 }
0089 
0090 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
0091 {
0092     int max_queue;
0093 
0094     if (get_user(max_queue, uarg))
0095         return -EFAULT;
0096     if (max_queue < 1)
0097         return -EINVAL;
0098     WRITE_ONCE(bd->max_queue, max_queue);
0099     return 0;
0100 }
0101 
0102 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0103 {
0104     struct bsg_device *bd = to_bsg_device(file_inode(file));
0105     struct request_queue *q = bd->queue;
0106     void __user *uarg = (void __user *) arg;
0107     int __user *intp = uarg;
0108     int val;
0109 
0110     switch (cmd) {
0111     /*
0112      * Our own ioctls
0113      */
0114     case SG_GET_COMMAND_Q:
0115         return bsg_get_command_q(bd, uarg);
0116     case SG_SET_COMMAND_Q:
0117         return bsg_set_command_q(bd, uarg);
0118 
0119     /*
0120      * SCSI/sg ioctls
0121      */
0122     case SG_GET_VERSION_NUM:
0123         return put_user(30527, intp);
0124     case SCSI_IOCTL_GET_IDLUN:
0125         return put_user(0, intp);
0126     case SCSI_IOCTL_GET_BUS_NUMBER:
0127         return put_user(0, intp);
0128     case SG_SET_TIMEOUT:
0129         if (get_user(val, intp))
0130             return -EFAULT;
0131         bd->timeout = clock_t_to_jiffies(val);
0132         return 0;
0133     case SG_GET_TIMEOUT:
0134         return jiffies_to_clock_t(bd->timeout);
0135     case SG_GET_RESERVED_SIZE:
0136         return put_user(min(bd->reserved_size, queue_max_bytes(q)),
0137                 intp);
0138     case SG_SET_RESERVED_SIZE:
0139         if (get_user(val, intp))
0140             return -EFAULT;
0141         if (val < 0)
0142             return -EINVAL;
0143         bd->reserved_size =
0144             min_t(unsigned int, val, queue_max_bytes(q));
0145         return 0;
0146     case SG_EMULATED_HOST:
0147         return put_user(1, intp);
0148     case SG_IO:
0149         return bsg_sg_io(bd, file->f_mode, uarg);
0150     case SCSI_IOCTL_SEND_COMMAND:
0151         pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
0152                 current->comm);
0153         return -EINVAL;
0154     default:
0155         return -ENOTTY;
0156     }
0157 }
0158 
0159 static const struct file_operations bsg_fops = {
0160     .open       =   bsg_open,
0161     .release    =   bsg_release,
0162     .unlocked_ioctl =   bsg_ioctl,
0163     .compat_ioctl   =   compat_ptr_ioctl,
0164     .owner      =   THIS_MODULE,
0165     .llseek     =   default_llseek,
0166 };
0167 
0168 static void bsg_device_release(struct device *dev)
0169 {
0170     struct bsg_device *bd = container_of(dev, struct bsg_device, device);
0171 
0172     ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
0173     kfree(bd);
0174 }
0175 
0176 void bsg_unregister_queue(struct bsg_device *bd)
0177 {
0178     if (bd->queue->kobj.sd)
0179         sysfs_remove_link(&bd->queue->kobj, "bsg");
0180     cdev_device_del(&bd->cdev, &bd->device);
0181     put_device(&bd->device);
0182 }
0183 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
0184 
0185 struct bsg_device *bsg_register_queue(struct request_queue *q,
0186         struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
0187 {
0188     struct bsg_device *bd;
0189     int ret;
0190 
0191     bd = kzalloc(sizeof(*bd), GFP_KERNEL);
0192     if (!bd)
0193         return ERR_PTR(-ENOMEM);
0194     bd->max_queue = BSG_DEFAULT_CMDS;
0195     bd->reserved_size = INT_MAX;
0196     bd->queue = q;
0197     bd->sg_io_fn = sg_io_fn;
0198 
0199     ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
0200     if (ret < 0) {
0201         if (ret == -ENOSPC)
0202             dev_err(parent, "bsg: too many bsg devices\n");
0203         kfree(bd);
0204         return ERR_PTR(ret);
0205     }
0206     bd->device.devt = MKDEV(bsg_major, ret);
0207     bd->device.class = bsg_class;
0208     bd->device.parent = parent;
0209     bd->device.release = bsg_device_release;
0210     dev_set_name(&bd->device, "%s", name);
0211     device_initialize(&bd->device);
0212 
0213     cdev_init(&bd->cdev, &bsg_fops);
0214     bd->cdev.owner = THIS_MODULE;
0215     ret = cdev_device_add(&bd->cdev, &bd->device);
0216     if (ret)
0217         goto out_put_device;
0218 
0219     if (q->kobj.sd) {
0220         ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg");
0221         if (ret)
0222             goto out_device_del;
0223     }
0224 
0225     return bd;
0226 
0227 out_device_del:
0228     cdev_device_del(&bd->cdev, &bd->device);
0229 out_put_device:
0230     put_device(&bd->device);
0231     return ERR_PTR(ret);
0232 }
0233 EXPORT_SYMBOL_GPL(bsg_register_queue);
0234 
0235 static char *bsg_devnode(struct device *dev, umode_t *mode)
0236 {
0237     return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
0238 }
0239 
0240 static int __init bsg_init(void)
0241 {
0242     dev_t devid;
0243     int ret;
0244 
0245     bsg_class = class_create(THIS_MODULE, "bsg");
0246     if (IS_ERR(bsg_class))
0247         return PTR_ERR(bsg_class);
0248     bsg_class->devnode = bsg_devnode;
0249 
0250     ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
0251     if (ret)
0252         goto destroy_bsg_class;
0253     bsg_major = MAJOR(devid);
0254 
0255     printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
0256            " loaded (major %d)\n", bsg_major);
0257     return 0;
0258 
0259 destroy_bsg_class:
0260     class_destroy(bsg_class);
0261     return ret;
0262 }
0263 
0264 MODULE_AUTHOR("Jens Axboe");
0265 MODULE_DESCRIPTION(BSG_DESCRIPTION);
0266 MODULE_LICENSE("GPL");
0267 
0268 device_initcall(bsg_init);