• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/pm_runtime.h>
16 
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_ioctl.h>
19 #include <scsi/scsi_cmnd.h>
20 #include <scsi/scsi_device.h>
21 #include <scsi/scsi_driver.h>
22 #include <scsi/sg.h>
23 
24 #define BSG_DESCRIPTION	"Block layer SCSI generic (bsg) driver"
25 #define BSG_VERSION	"0.4"
26 
27 #define bsg_dbg(bd, fmt, ...) \
28 	pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
29 
30 struct bsg_device {
31 	struct request_queue *queue;
32 	spinlock_t lock;
33 	struct hlist_node dev_list;
34 	refcount_t ref_count;
35 	char name[20];
36 	int max_queue;
37 };
38 
39 #define BSG_DEFAULT_CMDS	64
40 #define BSG_MAX_DEVS		32768
41 
42 static DEFINE_MUTEX(bsg_mutex);
43 static DEFINE_IDR(bsg_minor_idr);
44 
45 #define BSG_LIST_ARRAY_SIZE	8
46 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
47 
48 static struct class *bsg_class;
49 static int bsg_major;
50 
bsg_dev_idx_hash(int index)51 static inline struct hlist_head *bsg_dev_idx_hash(int index)
52 {
53 	return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
54 }
55 
56 #define uptr64(val) ((void __user *)(uintptr_t)(val))
57 
bsg_scsi_check_proto(struct sg_io_v4 * hdr)58 static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
59 {
60 	if (hdr->protocol != BSG_PROTOCOL_SCSI  ||
61 	    hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
62 		return -EINVAL;
63 	return 0;
64 }
65 
bsg_scsi_fill_hdr(struct request * rq,struct sg_io_v4 * hdr,fmode_t mode)66 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
67 		fmode_t mode)
68 {
69 	struct scsi_request *sreq = scsi_req(rq);
70 
71 	if (hdr->dout_xfer_len && hdr->din_xfer_len) {
72 		pr_warn_once("BIDI support in bsg has been removed.\n");
73 		return -EOPNOTSUPP;
74 	}
75 
76 	sreq->cmd_len = hdr->request_len;
77 	if (sreq->cmd_len > BLK_MAX_CDB) {
78 		sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
79 		if (!sreq->cmd)
80 			return -ENOMEM;
81 	}
82 
83 	if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
84 		return -EFAULT;
85 	if (blk_verify_command(sreq->cmd, mode))
86 		return -EPERM;
87 	return 0;
88 }
89 
bsg_scsi_complete_rq(struct request * rq,struct sg_io_v4 * hdr)90 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
91 {
92 	struct scsi_request *sreq = scsi_req(rq);
93 	int ret = 0;
94 
95 	/*
96 	 * fill in all the output members
97 	 */
98 	hdr->device_status = sreq->result & 0xff;
99 	hdr->transport_status = host_byte(sreq->result);
100 	hdr->driver_status = driver_byte(sreq->result);
101 	hdr->info = 0;
102 	if (hdr->device_status || hdr->transport_status || hdr->driver_status)
103 		hdr->info |= SG_INFO_CHECK;
104 	hdr->response_len = 0;
105 
106 	if (sreq->sense_len && hdr->response) {
107 		int len = min_t(unsigned int, hdr->max_response_len,
108 					sreq->sense_len);
109 
110 		if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
111 			ret = -EFAULT;
112 		else
113 			hdr->response_len = len;
114 	}
115 
116 	if (rq_data_dir(rq) == READ)
117 		hdr->din_resid = sreq->resid_len;
118 	else
119 		hdr->dout_resid = sreq->resid_len;
120 
121 	return ret;
122 }
123 
bsg_scsi_free_rq(struct request * rq)124 static void bsg_scsi_free_rq(struct request *rq)
125 {
126 	scsi_req_free_cmd(scsi_req(rq));
127 }
128 
129 static const struct bsg_ops bsg_scsi_ops = {
130 	.check_proto		= bsg_scsi_check_proto,
131 	.fill_hdr		= bsg_scsi_fill_hdr,
132 	.complete_rq		= bsg_scsi_complete_rq,
133 	.free_rq		= bsg_scsi_free_rq,
134 };
135 
bsg_sg_io(struct request_queue * q,fmode_t mode,void __user * uarg)136 static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
137 {
138 	struct request *rq;
139 	struct bio *bio;
140 	struct sg_io_v4 hdr;
141 	int ret;
142 
143 	if (copy_from_user(&hdr, uarg, sizeof(hdr)))
144 		return -EFAULT;
145 
146 	if (!q->bsg_dev.class_dev)
147 		return -ENXIO;
148 
149 	if (hdr.guard != 'Q')
150 		return -EINVAL;
151 	ret = q->bsg_dev.ops->check_proto(&hdr);
152 	if (ret)
153 		return ret;
154 
155 	rq = blk_get_request(q, hdr.dout_xfer_len ?
156 			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
157 	if (IS_ERR(rq))
158 		return PTR_ERR(rq);
159 
160 	ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
161 	if (ret) {
162 		blk_put_request(rq);
163 		return ret;
164 	}
165 
166 	rq->timeout = msecs_to_jiffies(hdr.timeout);
167 	if (!rq->timeout)
168 		rq->timeout = q->sg_timeout;
169 	if (!rq->timeout)
170 		rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
171 	if (rq->timeout < BLK_MIN_SG_TIMEOUT)
172 		rq->timeout = BLK_MIN_SG_TIMEOUT;
173 
174 	if (hdr.dout_xfer_len) {
175 		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
176 				hdr.dout_xfer_len, GFP_KERNEL);
177 	} else if (hdr.din_xfer_len) {
178 		ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
179 				hdr.din_xfer_len, GFP_KERNEL);
180 	}
181 
182 	if (ret)
183 		goto out_free_rq;
184 
185 	bio = rq->bio;
186 
187 	blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
188 	ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
189 	blk_rq_unmap_user(bio);
190 
191 out_free_rq:
192 	rq->q->bsg_dev.ops->free_rq(rq);
193 	blk_put_request(rq);
194 	if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
195 		return -EFAULT;
196 	return ret;
197 }
198 
bsg_alloc_device(void)199 static struct bsg_device *bsg_alloc_device(void)
200 {
201 	struct bsg_device *bd;
202 
203 	bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
204 	if (unlikely(!bd))
205 		return NULL;
206 
207 	spin_lock_init(&bd->lock);
208 	bd->max_queue = BSG_DEFAULT_CMDS;
209 	INIT_HLIST_NODE(&bd->dev_list);
210 	return bd;
211 }
212 
bsg_put_device(struct bsg_device * bd)213 static int bsg_put_device(struct bsg_device *bd)
214 {
215 	struct request_queue *q = bd->queue;
216 
217 	mutex_lock(&bsg_mutex);
218 
219 	if (!refcount_dec_and_test(&bd->ref_count)) {
220 		mutex_unlock(&bsg_mutex);
221 		return 0;
222 	}
223 
224 	hlist_del(&bd->dev_list);
225 	mutex_unlock(&bsg_mutex);
226 
227 	bsg_dbg(bd, "tearing down\n");
228 
229 	/*
230 	 * close can always block
231 	 */
232 	kfree(bd);
233 	blk_put_queue(q);
234 	return 0;
235 }
236 
bsg_add_device(struct inode * inode,struct request_queue * rq,struct file * file)237 static struct bsg_device *bsg_add_device(struct inode *inode,
238 					 struct request_queue *rq,
239 					 struct file *file)
240 {
241 	struct bsg_device *bd;
242 	unsigned char buf[32];
243 
244 	lockdep_assert_held(&bsg_mutex);
245 
246 	if (!blk_get_queue(rq))
247 		return ERR_PTR(-ENXIO);
248 
249 	bd = bsg_alloc_device();
250 	if (!bd) {
251 		blk_put_queue(rq);
252 		return ERR_PTR(-ENOMEM);
253 	}
254 
255 	bd->queue = rq;
256 
257 	refcount_set(&bd->ref_count, 1);
258 	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
259 
260 	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
261 	bsg_dbg(bd, "bound to <%s>, max queue %d\n",
262 		format_dev_t(buf, inode->i_rdev), bd->max_queue);
263 
264 	return bd;
265 }
266 
__bsg_get_device(int minor,struct request_queue * q)267 static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
268 {
269 	struct bsg_device *bd;
270 
271 	lockdep_assert_held(&bsg_mutex);
272 
273 	hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
274 		if (bd->queue == q) {
275 			refcount_inc(&bd->ref_count);
276 			goto found;
277 		}
278 	}
279 	bd = NULL;
280 found:
281 	return bd;
282 }
283 
bsg_get_device(struct inode * inode,struct file * file)284 static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
285 {
286 	struct bsg_device *bd;
287 	struct bsg_class_device *bcd;
288 
289 	/*
290 	 * find the class device
291 	 */
292 	mutex_lock(&bsg_mutex);
293 	bcd = idr_find(&bsg_minor_idr, iminor(inode));
294 
295 	if (!bcd) {
296 		bd = ERR_PTR(-ENODEV);
297 		goto out_unlock;
298 	}
299 
300 	bd = __bsg_get_device(iminor(inode), bcd->queue);
301 	if (!bd)
302 		bd = bsg_add_device(inode, bcd->queue, file);
303 
304 out_unlock:
305 	mutex_unlock(&bsg_mutex);
306 	return bd;
307 }
308 
bsg_open(struct inode * inode,struct file * file)309 static int bsg_open(struct inode *inode, struct file *file)
310 {
311 	struct bsg_device *bd;
312 	struct bsg_class_device *bcd;
313 
314 	bd = bsg_get_device(inode, file);
315 
316 	if (IS_ERR(bd))
317 		return PTR_ERR(bd);
318 
319 	bcd = &bd->queue->bsg_dev;
320 	pm_runtime_get_sync(bcd->class_dev->parent);
321 	file->private_data = bd;
322 	return 0;
323 }
324 
bsg_release(struct inode * inode,struct file * file)325 static int bsg_release(struct inode *inode, struct file *file)
326 {
327 	struct bsg_device *bd = file->private_data;
328 	struct bsg_class_device *bcd;
329 
330 	file->private_data = NULL;
331 
332 	bcd = &bd->queue->bsg_dev;
333 	pm_runtime_put_sync(bcd->class_dev->parent);
334 	return bsg_put_device(bd);
335 }
336 
bsg_get_command_q(struct bsg_device * bd,int __user * uarg)337 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
338 {
339 	return put_user(bd->max_queue, uarg);
340 }
341 
bsg_set_command_q(struct bsg_device * bd,int __user * uarg)342 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
343 {
344 	int queue;
345 
346 	if (get_user(queue, uarg))
347 		return -EFAULT;
348 	if (queue < 1)
349 		return -EINVAL;
350 
351 	spin_lock_irq(&bd->lock);
352 	bd->max_queue = queue;
353 	spin_unlock_irq(&bd->lock);
354 	return 0;
355 }
356 
bsg_ioctl(struct file * file,unsigned int cmd,unsigned long arg)357 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
358 {
359 	struct bsg_device *bd = file->private_data;
360 	void __user *uarg = (void __user *) arg;
361 
362 	switch (cmd) {
363 	/*
364 	 * Our own ioctls
365 	 */
366 	case SG_GET_COMMAND_Q:
367 		return bsg_get_command_q(bd, uarg);
368 	case SG_SET_COMMAND_Q:
369 		return bsg_set_command_q(bd, uarg);
370 
371 	/*
372 	 * SCSI/sg ioctls
373 	 */
374 	case SG_GET_VERSION_NUM:
375 	case SCSI_IOCTL_GET_IDLUN:
376 	case SCSI_IOCTL_GET_BUS_NUMBER:
377 	case SG_SET_TIMEOUT:
378 	case SG_GET_TIMEOUT:
379 	case SG_GET_RESERVED_SIZE:
380 	case SG_SET_RESERVED_SIZE:
381 	case SG_EMULATED_HOST:
382 		return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
383 	case SG_IO:
384 		return bsg_sg_io(bd->queue, file->f_mode, uarg);
385 	case SCSI_IOCTL_SEND_COMMAND:
386 		pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
387 				current->comm);
388 		return -EINVAL;
389 	default:
390 		return -ENOTTY;
391 	}
392 }
393 
394 static const struct file_operations bsg_fops = {
395 	.open		=	bsg_open,
396 	.release	=	bsg_release,
397 	.unlocked_ioctl	=	bsg_ioctl,
398 	.compat_ioctl	=	compat_ptr_ioctl,
399 	.owner		=	THIS_MODULE,
400 	.llseek		=	default_llseek,
401 };
402 
bsg_unregister_queue(struct request_queue * q)403 void bsg_unregister_queue(struct request_queue *q)
404 {
405 	struct bsg_class_device *bcd = &q->bsg_dev;
406 
407 	if (!bcd->class_dev)
408 		return;
409 
410 	mutex_lock(&bsg_mutex);
411 	idr_remove(&bsg_minor_idr, bcd->minor);
412 	if (q->kobj.sd)
413 		sysfs_remove_link(&q->kobj, "bsg");
414 	device_unregister(bcd->class_dev);
415 	bcd->class_dev = NULL;
416 	mutex_unlock(&bsg_mutex);
417 }
418 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
419 
bsg_register_queue(struct request_queue * q,struct device * parent,const char * name,const struct bsg_ops * ops)420 int bsg_register_queue(struct request_queue *q, struct device *parent,
421 		const char *name, const struct bsg_ops *ops)
422 {
423 	struct bsg_class_device *bcd;
424 	dev_t dev;
425 	int ret;
426 	struct device *class_dev = NULL;
427 
428 	/*
429 	 * we need a proper transport to send commands, not a stacked device
430 	 */
431 	if (!queue_is_mq(q))
432 		return 0;
433 
434 	bcd = &q->bsg_dev;
435 	memset(bcd, 0, sizeof(*bcd));
436 
437 	mutex_lock(&bsg_mutex);
438 
439 	ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
440 	if (ret < 0) {
441 		if (ret == -ENOSPC) {
442 			printk(KERN_ERR "bsg: too many bsg devices\n");
443 			ret = -EINVAL;
444 		}
445 		goto unlock;
446 	}
447 
448 	bcd->minor = ret;
449 	bcd->queue = q;
450 	bcd->ops = ops;
451 	dev = MKDEV(bsg_major, bcd->minor);
452 	class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
453 	if (IS_ERR(class_dev)) {
454 		ret = PTR_ERR(class_dev);
455 		goto idr_remove;
456 	}
457 	bcd->class_dev = class_dev;
458 
459 	if (q->kobj.sd) {
460 		ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
461 		if (ret)
462 			goto unregister_class_dev;
463 	}
464 
465 	mutex_unlock(&bsg_mutex);
466 	return 0;
467 
468 unregister_class_dev:
469 	device_unregister(class_dev);
470 idr_remove:
471 	idr_remove(&bsg_minor_idr, bcd->minor);
472 unlock:
473 	mutex_unlock(&bsg_mutex);
474 	return ret;
475 }
476 
bsg_scsi_register_queue(struct request_queue * q,struct device * parent)477 int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
478 {
479 	if (!blk_queue_scsi_passthrough(q)) {
480 		WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
481 		return -EINVAL;
482 	}
483 
484 	return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
485 }
486 EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
487 
488 static struct cdev bsg_cdev;
489 
bsg_devnode(struct device * dev,umode_t * mode)490 static char *bsg_devnode(struct device *dev, umode_t *mode)
491 {
492 	return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
493 }
494 
bsg_init(void)495 static int __init bsg_init(void)
496 {
497 	int ret, i;
498 	dev_t devid;
499 
500 	for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
501 		INIT_HLIST_HEAD(&bsg_device_list[i]);
502 
503 	bsg_class = class_create(THIS_MODULE, "bsg");
504 	if (IS_ERR(bsg_class))
505 		return PTR_ERR(bsg_class);
506 	bsg_class->devnode = bsg_devnode;
507 
508 	ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
509 	if (ret)
510 		goto destroy_bsg_class;
511 
512 	bsg_major = MAJOR(devid);
513 
514 	cdev_init(&bsg_cdev, &bsg_fops);
515 	ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
516 	if (ret)
517 		goto unregister_chrdev;
518 
519 	printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
520 	       " loaded (major %d)\n", bsg_major);
521 	return 0;
522 unregister_chrdev:
523 	unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
524 destroy_bsg_class:
525 	class_destroy(bsg_class);
526 	return ret;
527 }
528 
529 MODULE_AUTHOR("Jens Axboe");
530 MODULE_DESCRIPTION(BSG_DESCRIPTION);
531 MODULE_LICENSE("GPL");
532 
533 device_initcall(bsg_init);
534