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