xref: /linux/block/bsg.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bsg.c - block layer implementation of the sg v4 interface
4  */
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>
15 #include <linux/io_uring/cmd.h>
16 
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_ioctl.h>
19 #include <scsi/sg.h>
20 
21 #define BSG_DESCRIPTION	"Block layer SCSI generic (bsg) driver"
22 #define BSG_VERSION	"0.4"
23 
24 struct bsg_device {
25 	struct request_queue *queue;
26 	struct device device;
27 	struct cdev cdev;
28 	int max_queue;
29 	unsigned int timeout;
30 	unsigned int reserved_size;
31 	bsg_sg_io_fn *sg_io_fn;
32 	bsg_uring_cmd_fn *uring_cmd_fn;
33 };
34 
35 static inline struct bsg_device *to_bsg_device(struct inode *inode)
36 {
37 	return container_of(inode->i_cdev, struct bsg_device, cdev);
38 }
39 
40 #define BSG_DEFAULT_CMDS	64
41 #define BSG_MAX_DEVS		(1 << MINORBITS)
42 
43 static DEFINE_IDA(bsg_minor_ida);
44 static const struct class bsg_class;
45 static int bsg_major;
46 
47 static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
48 {
49 	unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
50 
51 	if (hdr->timeout)
52 		timeout = msecs_to_jiffies(hdr->timeout);
53 	else if (bd->timeout)
54 		timeout = bd->timeout;
55 
56 	return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
57 }
58 
59 static int bsg_sg_io(struct bsg_device *bd, bool open_for_write,
60 		     void __user *uarg)
61 {
62 	struct sg_io_v4 hdr;
63 	int ret;
64 
65 	if (copy_from_user(&hdr, uarg, sizeof(hdr)))
66 		return -EFAULT;
67 	if (hdr.guard != 'Q')
68 		return -EINVAL;
69 	ret = bd->sg_io_fn(bd->queue, &hdr, open_for_write,
70 			   bsg_timeout(bd, &hdr));
71 	if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
72 		return -EFAULT;
73 	return ret;
74 }
75 
76 static int bsg_open(struct inode *inode, struct file *file)
77 {
78 	if (!blk_get_queue(to_bsg_device(inode)->queue))
79 		return -ENXIO;
80 	return 0;
81 }
82 
83 static int bsg_release(struct inode *inode, struct file *file)
84 {
85 	blk_put_queue(to_bsg_device(inode)->queue);
86 	return 0;
87 }
88 
89 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
90 {
91 	return put_user(READ_ONCE(bd->max_queue), uarg);
92 }
93 
94 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
95 {
96 	int max_queue;
97 
98 	if (get_user(max_queue, uarg))
99 		return -EFAULT;
100 	if (max_queue < 1)
101 		return -EINVAL;
102 	WRITE_ONCE(bd->max_queue, max_queue);
103 	return 0;
104 }
105 
106 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
107 {
108 	struct bsg_device *bd = to_bsg_device(file_inode(file));
109 	struct request_queue *q = bd->queue;
110 	void __user *uarg = (void __user *) arg;
111 	int __user *intp = uarg;
112 	int val;
113 
114 	switch (cmd) {
115 	/*
116 	 * Our own ioctls
117 	 */
118 	case SG_GET_COMMAND_Q:
119 		return bsg_get_command_q(bd, uarg);
120 	case SG_SET_COMMAND_Q:
121 		return bsg_set_command_q(bd, uarg);
122 
123 	/*
124 	 * SCSI/sg ioctls
125 	 */
126 	case SG_GET_VERSION_NUM:
127 		return put_user(30527, intp);
128 	case SCSI_IOCTL_GET_IDLUN:
129 		return put_user(0, intp);
130 	case SCSI_IOCTL_GET_BUS_NUMBER:
131 		return put_user(0, intp);
132 	case SG_SET_TIMEOUT:
133 		if (get_user(val, intp))
134 			return -EFAULT;
135 		if (val < 0)
136 			return -EIO;
137 		bd->timeout = min(clock_t_to_jiffies(val),
138 				  min(UINT_MAX, MAX_JIFFY_OFFSET));
139 		return 0;
140 	case SG_GET_TIMEOUT:
141 		return jiffies_to_clock_t(bd->timeout);
142 	case SG_GET_RESERVED_SIZE:
143 		return put_user(min(bd->reserved_size, queue_max_bytes(q)),
144 				intp);
145 	case SG_SET_RESERVED_SIZE:
146 		if (get_user(val, intp))
147 			return -EFAULT;
148 		if (val < 0)
149 			return -EINVAL;
150 		bd->reserved_size =
151 			min_t(unsigned int, val, queue_max_bytes(q));
152 		return 0;
153 	case SG_EMULATED_HOST:
154 		return put_user(1, intp);
155 	case SG_IO:
156 		return bsg_sg_io(bd, file->f_mode & FMODE_WRITE, uarg);
157 	case SCSI_IOCTL_SEND_COMMAND:
158 		pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
159 				current->comm);
160 		return -EINVAL;
161 	default:
162 		return -ENOTTY;
163 	}
164 }
165 
166 static int bsg_check_uring_features(unsigned int issue_flags)
167 {
168 	/* BSG passthrough requires big SQE/CQE support */
169 	if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
170 	    (IO_URING_F_SQE128|IO_URING_F_CQE32))
171 		return -EOPNOTSUPP;
172 	return 0;
173 }
174 
175 static int bsg_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
176 {
177 	struct bsg_device *bd = to_bsg_device(file_inode(ioucmd->file));
178 	bool open_for_write = ioucmd->file->f_mode & FMODE_WRITE;
179 	struct request_queue *q = bd->queue;
180 	int ret;
181 
182 	ret = bsg_check_uring_features(issue_flags);
183 	if (ret)
184 		return ret;
185 
186 	if (!bd->uring_cmd_fn)
187 		return -EOPNOTSUPP;
188 
189 	return bd->uring_cmd_fn(q, ioucmd, issue_flags, open_for_write);
190 }
191 
192 static const struct file_operations bsg_fops = {
193 	.open		=	bsg_open,
194 	.release	=	bsg_release,
195 	.unlocked_ioctl	=	bsg_ioctl,
196 	.compat_ioctl	=	compat_ptr_ioctl,
197 	.uring_cmd	=	bsg_uring_cmd,
198 	.owner		=	THIS_MODULE,
199 	.llseek		=	default_llseek,
200 };
201 
202 static void bsg_device_release(struct device *dev)
203 {
204 	struct bsg_device *bd = container_of(dev, struct bsg_device, device);
205 
206 	ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
207 	kfree(bd);
208 }
209 
210 void bsg_unregister_queue(struct bsg_device *bd)
211 {
212 	struct gendisk *disk = bd->queue->disk;
213 
214 	if (disk && disk->queue_kobj.sd)
215 		sysfs_remove_link(&disk->queue_kobj, "bsg");
216 	cdev_device_del(&bd->cdev, &bd->device);
217 	put_device(&bd->device);
218 }
219 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
220 
221 struct bsg_device *bsg_register_queue(struct request_queue *q,
222 		struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn,
223 		bsg_uring_cmd_fn *uring_cmd_fn)
224 {
225 	struct bsg_device *bd;
226 	int ret;
227 
228 	bd = kzalloc_obj(*bd);
229 	if (!bd)
230 		return ERR_PTR(-ENOMEM);
231 	bd->max_queue = BSG_DEFAULT_CMDS;
232 	bd->reserved_size = INT_MAX;
233 	bd->queue = q;
234 	bd->sg_io_fn = sg_io_fn;
235 	bd->uring_cmd_fn = uring_cmd_fn;
236 
237 	ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
238 	if (ret < 0) {
239 		if (ret == -ENOSPC)
240 			dev_err(parent, "bsg: too many bsg devices\n");
241 		kfree(bd);
242 		return ERR_PTR(ret);
243 	}
244 	bd->device.devt = MKDEV(bsg_major, ret);
245 	bd->device.class = &bsg_class;
246 	bd->device.parent = parent;
247 	bd->device.release = bsg_device_release;
248 	dev_set_name(&bd->device, "%s", name);
249 	device_initialize(&bd->device);
250 
251 	cdev_init(&bd->cdev, &bsg_fops);
252 	bd->cdev.owner = THIS_MODULE;
253 	ret = cdev_device_add(&bd->cdev, &bd->device);
254 	if (ret)
255 		goto out_put_device;
256 
257 	if (q->disk && q->disk->queue_kobj.sd) {
258 		ret = sysfs_create_link(&q->disk->queue_kobj, &bd->device.kobj,
259 					"bsg");
260 		if (ret)
261 			goto out_device_del;
262 	}
263 
264 	return bd;
265 
266 out_device_del:
267 	cdev_device_del(&bd->cdev, &bd->device);
268 out_put_device:
269 	put_device(&bd->device);
270 	return ERR_PTR(ret);
271 }
272 EXPORT_SYMBOL_GPL(bsg_register_queue);
273 
274 static char *bsg_devnode(const struct device *dev, umode_t *mode)
275 {
276 	return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
277 }
278 
279 static const struct class bsg_class = {
280 	.name		= "bsg",
281 	.devnode	= bsg_devnode,
282 };
283 
284 static int __init bsg_init(void)
285 {
286 	dev_t devid;
287 	int ret;
288 
289 	ret = class_register(&bsg_class);
290 	if (ret)
291 		return ret;
292 
293 	ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
294 	if (ret)
295 		goto destroy_bsg_class;
296 	bsg_major = MAJOR(devid);
297 
298 	printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
299 	       " loaded (major %d)\n", bsg_major);
300 	return 0;
301 
302 destroy_bsg_class:
303 	class_unregister(&bsg_class);
304 	return ret;
305 }
306 
307 MODULE_AUTHOR("Jens Axboe");
308 MODULE_DESCRIPTION(BSG_DESCRIPTION);
309 MODULE_LICENSE("GPL");
310 
311 device_initcall(bsg_init);
312