1 // SPDX-License-Identifier: GPL-2.0
3 * bsg.c - block layer implementation of the sg v4 interface
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/file.h>
8 #include <linux/blkdev.h>
9 #include <linux/cdev.h>
10 #include <linux/jiffies.h>
11 #include <linux/percpu.h>
12 #include <linux/idr.h>
13 #include <linux/bsg.h>
14 #include <linux/slab.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_ioctl.h>
20 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
21 #define BSG_VERSION "0.4"
24 struct request_queue *queue;
29 unsigned int reserved_size;
30 bsg_sg_io_fn *sg_io_fn;
33 static inline struct bsg_device *to_bsg_device(struct inode *inode)
35 return container_of(inode->i_cdev, struct bsg_device, cdev);
38 #define BSG_DEFAULT_CMDS 64
39 #define BSG_MAX_DEVS 32768
41 static DEFINE_IDA(bsg_minor_ida);
42 static struct class *bsg_class;
45 static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
47 unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
50 timeout = msecs_to_jiffies(hdr->timeout);
52 timeout = bd->timeout;
54 return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
57 static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg)
62 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
66 ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr));
67 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
72 static int bsg_open(struct inode *inode, struct file *file)
74 if (!blk_get_queue(to_bsg_device(inode)->queue))
79 static int bsg_release(struct inode *inode, struct file *file)
81 blk_put_queue(to_bsg_device(inode)->queue);
85 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
87 return put_user(READ_ONCE(bd->max_queue), uarg);
90 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
94 if (get_user(max_queue, uarg))
98 WRITE_ONCE(bd->max_queue, max_queue);
102 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
104 struct bsg_device *bd = to_bsg_device(file_inode(file));
105 struct request_queue *q = bd->queue;
106 void __user *uarg = (void __user *) arg;
107 int __user *intp = uarg;
114 case SG_GET_COMMAND_Q:
115 return bsg_get_command_q(bd, uarg);
116 case SG_SET_COMMAND_Q:
117 return bsg_set_command_q(bd, uarg);
122 case SG_GET_VERSION_NUM:
123 return put_user(30527, intp);
124 case SCSI_IOCTL_GET_IDLUN:
125 return put_user(0, intp);
126 case SCSI_IOCTL_GET_BUS_NUMBER:
127 return put_user(0, intp);
129 if (get_user(val, intp))
131 bd->timeout = clock_t_to_jiffies(val);
134 return jiffies_to_clock_t(bd->timeout);
135 case SG_GET_RESERVED_SIZE:
136 return put_user(min(bd->reserved_size, queue_max_bytes(q)),
138 case SG_SET_RESERVED_SIZE:
139 if (get_user(val, intp))
144 min_t(unsigned int, val, queue_max_bytes(q));
146 case SG_EMULATED_HOST:
147 return put_user(1, intp);
149 return bsg_sg_io(bd, file->f_mode, uarg);
150 case SCSI_IOCTL_SEND_COMMAND:
151 pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
159 static const struct file_operations bsg_fops = {
161 .release = bsg_release,
162 .unlocked_ioctl = bsg_ioctl,
163 .compat_ioctl = compat_ptr_ioctl,
164 .owner = THIS_MODULE,
165 .llseek = default_llseek,
168 static void bsg_device_release(struct device *dev)
170 struct bsg_device *bd = container_of(dev, struct bsg_device, device);
172 ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
176 void bsg_unregister_queue(struct bsg_device *bd)
178 struct gendisk *disk = bd->queue->disk;
180 if (disk && disk->queue_kobj.sd)
181 sysfs_remove_link(&disk->queue_kobj, "bsg");
182 cdev_device_del(&bd->cdev, &bd->device);
183 put_device(&bd->device);
185 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
187 struct bsg_device *bsg_register_queue(struct request_queue *q,
188 struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
190 struct bsg_device *bd;
193 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
195 return ERR_PTR(-ENOMEM);
196 bd->max_queue = BSG_DEFAULT_CMDS;
197 bd->reserved_size = INT_MAX;
199 bd->sg_io_fn = sg_io_fn;
201 ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
204 dev_err(parent, "bsg: too many bsg devices\n");
208 bd->device.devt = MKDEV(bsg_major, ret);
209 bd->device.class = bsg_class;
210 bd->device.parent = parent;
211 bd->device.release = bsg_device_release;
212 dev_set_name(&bd->device, "%s", name);
213 device_initialize(&bd->device);
215 cdev_init(&bd->cdev, &bsg_fops);
216 bd->cdev.owner = THIS_MODULE;
217 ret = cdev_device_add(&bd->cdev, &bd->device);
221 if (q->disk && q->disk->queue_kobj.sd) {
222 ret = sysfs_create_link(&q->disk->queue_kobj, &bd->device.kobj,
231 cdev_device_del(&bd->cdev, &bd->device);
233 put_device(&bd->device);
236 EXPORT_SYMBOL_GPL(bsg_register_queue);
238 static char *bsg_devnode(struct device *dev, umode_t *mode)
240 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
243 static int __init bsg_init(void)
248 bsg_class = class_create(THIS_MODULE, "bsg");
249 if (IS_ERR(bsg_class))
250 return PTR_ERR(bsg_class);
251 bsg_class->devnode = bsg_devnode;
253 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
255 goto destroy_bsg_class;
256 bsg_major = MAJOR(devid);
258 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
259 " loaded (major %d)\n", bsg_major);
263 class_destroy(bsg_class);
267 MODULE_AUTHOR("Jens Axboe");
268 MODULE_DESCRIPTION(BSG_DESCRIPTION);
269 MODULE_LICENSE("GPL");
271 device_initcall(bsg_init);