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>
18 #include <scsi/scsi_cmnd.h>
19 #include <scsi/scsi_device.h>
20 #include <scsi/scsi_driver.h>
23 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
24 #define BSG_VERSION "0.4"
26 #define bsg_dbg(bd, fmt, ...) \
27 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
30 struct request_queue *queue;
32 struct hlist_node dev_list;
38 #define BSG_DEFAULT_CMDS 64
39 #define BSG_MAX_DEVS 32768
41 static DEFINE_MUTEX(bsg_mutex);
42 static DEFINE_IDR(bsg_minor_idr);
44 #define BSG_LIST_ARRAY_SIZE 8
45 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
47 static struct class *bsg_class;
50 static inline struct hlist_head *bsg_dev_idx_hash(int index)
52 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
55 #define uptr64(val) ((void __user *)(uintptr_t)(val))
57 static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
59 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
60 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
65 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
68 struct scsi_request *sreq = scsi_req(rq);
70 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
71 pr_warn_once("BIDI support in bsg has been removed.\n");
75 sreq->cmd_len = hdr->request_len;
76 if (sreq->cmd_len > BLK_MAX_CDB) {
77 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
82 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
84 if (blk_verify_command(sreq->cmd, mode))
89 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
91 struct scsi_request *sreq = scsi_req(rq);
95 * fill in all the output members
97 hdr->device_status = sreq->result & 0xff;
98 hdr->transport_status = host_byte(sreq->result);
99 hdr->driver_status = driver_byte(sreq->result);
101 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
102 hdr->info |= SG_INFO_CHECK;
103 hdr->response_len = 0;
105 if (sreq->sense_len && hdr->response) {
106 int len = min_t(unsigned int, hdr->max_response_len,
109 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
112 hdr->response_len = len;
115 if (rq_data_dir(rq) == READ)
116 hdr->din_resid = sreq->resid_len;
118 hdr->dout_resid = sreq->resid_len;
123 static void bsg_scsi_free_rq(struct request *rq)
125 scsi_req_free_cmd(scsi_req(rq));
128 static const struct bsg_ops bsg_scsi_ops = {
129 .check_proto = bsg_scsi_check_proto,
130 .fill_hdr = bsg_scsi_fill_hdr,
131 .complete_rq = bsg_scsi_complete_rq,
132 .free_rq = bsg_scsi_free_rq,
135 static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
142 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
145 if (!q->bsg_dev.class_dev)
148 if (hdr.guard != 'Q')
150 ret = q->bsg_dev.ops->check_proto(&hdr);
154 rq = blk_get_request(q, hdr.dout_xfer_len ?
155 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
159 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
165 rq->timeout = msecs_to_jiffies(hdr.timeout);
167 rq->timeout = q->sg_timeout;
169 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
170 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
171 rq->timeout = BLK_MIN_SG_TIMEOUT;
173 if (hdr.dout_xfer_len) {
174 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
175 hdr.dout_xfer_len, GFP_KERNEL);
176 } else if (hdr.din_xfer_len) {
177 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
178 hdr.din_xfer_len, GFP_KERNEL);
186 blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
187 ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
188 blk_rq_unmap_user(bio);
191 rq->q->bsg_dev.ops->free_rq(rq);
193 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
198 static struct bsg_device *bsg_alloc_device(void)
200 struct bsg_device *bd;
202 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
206 spin_lock_init(&bd->lock);
207 bd->max_queue = BSG_DEFAULT_CMDS;
208 INIT_HLIST_NODE(&bd->dev_list);
212 static int bsg_put_device(struct bsg_device *bd)
214 struct request_queue *q = bd->queue;
216 mutex_lock(&bsg_mutex);
218 if (!refcount_dec_and_test(&bd->ref_count)) {
219 mutex_unlock(&bsg_mutex);
223 hlist_del(&bd->dev_list);
224 mutex_unlock(&bsg_mutex);
226 bsg_dbg(bd, "tearing down\n");
229 * close can always block
236 static struct bsg_device *bsg_add_device(struct inode *inode,
237 struct request_queue *rq,
240 struct bsg_device *bd;
241 unsigned char buf[32];
243 lockdep_assert_held(&bsg_mutex);
245 if (!blk_get_queue(rq))
246 return ERR_PTR(-ENXIO);
248 bd = bsg_alloc_device();
251 return ERR_PTR(-ENOMEM);
256 refcount_set(&bd->ref_count, 1);
257 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
259 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
260 bsg_dbg(bd, "bound to <%s>, max queue %d\n",
261 format_dev_t(buf, inode->i_rdev), bd->max_queue);
266 static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
268 struct bsg_device *bd;
270 lockdep_assert_held(&bsg_mutex);
272 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
273 if (bd->queue == q) {
274 refcount_inc(&bd->ref_count);
283 static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
285 struct bsg_device *bd;
286 struct bsg_class_device *bcd;
289 * find the class device
291 mutex_lock(&bsg_mutex);
292 bcd = idr_find(&bsg_minor_idr, iminor(inode));
295 bd = ERR_PTR(-ENODEV);
299 bd = __bsg_get_device(iminor(inode), bcd->queue);
301 bd = bsg_add_device(inode, bcd->queue, file);
304 mutex_unlock(&bsg_mutex);
308 static int bsg_open(struct inode *inode, struct file *file)
310 struct bsg_device *bd;
312 bd = bsg_get_device(inode, file);
317 file->private_data = bd;
321 static int bsg_release(struct inode *inode, struct file *file)
323 struct bsg_device *bd = file->private_data;
325 file->private_data = NULL;
326 return bsg_put_device(bd);
329 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
331 return put_user(bd->max_queue, uarg);
334 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
338 if (get_user(queue, uarg))
343 spin_lock_irq(&bd->lock);
344 bd->max_queue = queue;
345 spin_unlock_irq(&bd->lock);
349 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
351 struct bsg_device *bd = file->private_data;
352 void __user *uarg = (void __user *) arg;
358 case SG_GET_COMMAND_Q:
359 return bsg_get_command_q(bd, uarg);
360 case SG_SET_COMMAND_Q:
361 return bsg_set_command_q(bd, uarg);
366 case SG_GET_VERSION_NUM:
367 case SCSI_IOCTL_GET_IDLUN:
368 case SCSI_IOCTL_GET_BUS_NUMBER:
371 case SG_GET_RESERVED_SIZE:
372 case SG_SET_RESERVED_SIZE:
373 case SG_EMULATED_HOST:
374 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
376 return bsg_sg_io(bd->queue, file->f_mode, uarg);
377 case SCSI_IOCTL_SEND_COMMAND:
378 pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
386 static const struct file_operations bsg_fops = {
388 .release = bsg_release,
389 .unlocked_ioctl = bsg_ioctl,
390 .compat_ioctl = compat_ptr_ioctl,
391 .owner = THIS_MODULE,
392 .llseek = default_llseek,
395 void bsg_unregister_queue(struct request_queue *q)
397 struct bsg_class_device *bcd = &q->bsg_dev;
402 mutex_lock(&bsg_mutex);
403 idr_remove(&bsg_minor_idr, bcd->minor);
405 sysfs_remove_link(&q->kobj, "bsg");
406 device_unregister(bcd->class_dev);
407 bcd->class_dev = NULL;
408 mutex_unlock(&bsg_mutex);
410 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
412 int bsg_register_queue(struct request_queue *q, struct device *parent,
413 const char *name, const struct bsg_ops *ops)
415 struct bsg_class_device *bcd;
418 struct device *class_dev = NULL;
421 * we need a proper transport to send commands, not a stacked device
427 memset(bcd, 0, sizeof(*bcd));
429 mutex_lock(&bsg_mutex);
431 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
433 if (ret == -ENOSPC) {
434 printk(KERN_ERR "bsg: too many bsg devices\n");
443 dev = MKDEV(bsg_major, bcd->minor);
444 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
445 if (IS_ERR(class_dev)) {
446 ret = PTR_ERR(class_dev);
449 bcd->class_dev = class_dev;
452 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
454 goto unregister_class_dev;
457 mutex_unlock(&bsg_mutex);
460 unregister_class_dev:
461 device_unregister(class_dev);
463 idr_remove(&bsg_minor_idr, bcd->minor);
465 mutex_unlock(&bsg_mutex);
469 int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
471 if (!blk_queue_scsi_passthrough(q)) {
472 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
476 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
478 EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
480 static struct cdev bsg_cdev;
482 static char *bsg_devnode(struct device *dev, umode_t *mode)
484 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
487 static int __init bsg_init(void)
492 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
493 INIT_HLIST_HEAD(&bsg_device_list[i]);
495 bsg_class = class_create(THIS_MODULE, "bsg");
496 if (IS_ERR(bsg_class))
497 return PTR_ERR(bsg_class);
498 bsg_class->devnode = bsg_devnode;
500 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
502 goto destroy_bsg_class;
504 bsg_major = MAJOR(devid);
506 cdev_init(&bsg_cdev, &bsg_fops);
507 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
509 goto unregister_chrdev;
511 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
512 " loaded (major %d)\n", bsg_major);
515 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
517 class_destroy(bsg_class);
521 MODULE_AUTHOR("Jens Axboe");
522 MODULE_DESCRIPTION(BSG_DESCRIPTION);
523 MODULE_LICENSE("GPL");
525 device_initcall(bsg_init);