2 * bsg.c - block layer implementation of the sg v4 interface
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.
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>
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>
30 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
31 #define BSG_VERSION "0.4"
33 #define bsg_dbg(bd, fmt, ...) \
34 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
37 struct request_queue *queue;
39 struct hlist_node dev_list;
45 #define BSG_DEFAULT_CMDS 64
46 #define BSG_MAX_DEVS 32768
48 static DEFINE_MUTEX(bsg_mutex);
49 static DEFINE_IDR(bsg_minor_idr);
51 #define BSG_LIST_ARRAY_SIZE 8
52 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
54 static struct class *bsg_class;
57 static inline struct hlist_head *bsg_dev_idx_hash(int index)
59 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
62 #define uptr64(val) ((void __user *)(uintptr_t)(val))
64 static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
66 if (hdr->protocol != BSG_PROTOCOL_SCSI ||
67 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
72 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
75 struct scsi_request *sreq = scsi_req(rq);
77 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
78 pr_warn_once("BIDI support in bsg has been removed.\n");
82 sreq->cmd_len = hdr->request_len;
83 if (sreq->cmd_len > BLK_MAX_CDB) {
84 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
89 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
91 if (blk_verify_command(sreq->cmd, mode))
96 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
98 struct scsi_request *sreq = scsi_req(rq);
102 * fill in all the output members
104 hdr->device_status = sreq->result & 0xff;
105 hdr->transport_status = host_byte(sreq->result);
106 hdr->driver_status = driver_byte(sreq->result);
108 if (hdr->device_status || hdr->transport_status || hdr->driver_status)
109 hdr->info |= SG_INFO_CHECK;
110 hdr->response_len = 0;
112 if (sreq->sense_len && hdr->response) {
113 int len = min_t(unsigned int, hdr->max_response_len,
116 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
119 hdr->response_len = len;
122 if (rq_data_dir(rq) == READ)
123 hdr->din_resid = sreq->resid_len;
125 hdr->dout_resid = sreq->resid_len;
130 static void bsg_scsi_free_rq(struct request *rq)
132 scsi_req_free_cmd(scsi_req(rq));
135 static const struct bsg_ops bsg_scsi_ops = {
136 .check_proto = bsg_scsi_check_proto,
137 .fill_hdr = bsg_scsi_fill_hdr,
138 .complete_rq = bsg_scsi_complete_rq,
139 .free_rq = bsg_scsi_free_rq,
142 static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
149 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
152 if (!q->bsg_dev.class_dev)
155 if (hdr.guard != 'Q')
157 ret = q->bsg_dev.ops->check_proto(&hdr);
161 rq = blk_get_request(q, hdr.dout_xfer_len ?
162 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
166 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
170 rq->timeout = msecs_to_jiffies(hdr.timeout);
172 rq->timeout = q->sg_timeout;
174 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
175 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
176 rq->timeout = BLK_MIN_SG_TIMEOUT;
178 if (hdr.dout_xfer_len) {
179 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
180 hdr.dout_xfer_len, GFP_KERNEL);
181 } else if (hdr.din_xfer_len) {
182 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
183 hdr.din_xfer_len, GFP_KERNEL);
191 blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
192 ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
193 blk_rq_unmap_user(bio);
196 rq->q->bsg_dev.ops->free_rq(rq);
198 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
203 static struct bsg_device *bsg_alloc_device(void)
205 struct bsg_device *bd;
207 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
211 spin_lock_init(&bd->lock);
212 bd->max_queue = BSG_DEFAULT_CMDS;
213 INIT_HLIST_NODE(&bd->dev_list);
217 static int bsg_put_device(struct bsg_device *bd)
219 struct request_queue *q = bd->queue;
221 mutex_lock(&bsg_mutex);
223 if (!refcount_dec_and_test(&bd->ref_count)) {
224 mutex_unlock(&bsg_mutex);
228 hlist_del(&bd->dev_list);
229 mutex_unlock(&bsg_mutex);
231 bsg_dbg(bd, "tearing down\n");
234 * close can always block
241 static struct bsg_device *bsg_add_device(struct inode *inode,
242 struct request_queue *rq,
245 struct bsg_device *bd;
246 unsigned char buf[32];
248 lockdep_assert_held(&bsg_mutex);
250 if (!blk_get_queue(rq))
251 return ERR_PTR(-ENXIO);
253 bd = bsg_alloc_device();
256 return ERR_PTR(-ENOMEM);
261 refcount_set(&bd->ref_count, 1);
262 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
264 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
265 bsg_dbg(bd, "bound to <%s>, max queue %d\n",
266 format_dev_t(buf, inode->i_rdev), bd->max_queue);
271 static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
273 struct bsg_device *bd;
275 lockdep_assert_held(&bsg_mutex);
277 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
278 if (bd->queue == q) {
279 refcount_inc(&bd->ref_count);
288 static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
290 struct bsg_device *bd;
291 struct bsg_class_device *bcd;
294 * find the class device
296 mutex_lock(&bsg_mutex);
297 bcd = idr_find(&bsg_minor_idr, iminor(inode));
300 bd = ERR_PTR(-ENODEV);
304 bd = __bsg_get_device(iminor(inode), bcd->queue);
306 bd = bsg_add_device(inode, bcd->queue, file);
309 mutex_unlock(&bsg_mutex);
313 static int bsg_open(struct inode *inode, struct file *file)
315 struct bsg_device *bd;
317 bd = bsg_get_device(inode, file);
322 file->private_data = bd;
326 static int bsg_release(struct inode *inode, struct file *file)
328 struct bsg_device *bd = file->private_data;
330 file->private_data = NULL;
331 return bsg_put_device(bd);
334 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
336 return put_user(bd->max_queue, uarg);
339 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
343 if (get_user(queue, uarg))
348 spin_lock_irq(&bd->lock);
349 bd->max_queue = queue;
350 spin_unlock_irq(&bd->lock);
354 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
356 struct bsg_device *bd = file->private_data;
357 void __user *uarg = (void __user *) arg;
363 case SG_GET_COMMAND_Q:
364 return bsg_get_command_q(bd, uarg);
365 case SG_SET_COMMAND_Q:
366 return bsg_set_command_q(bd, uarg);
371 case SG_GET_VERSION_NUM:
372 case SCSI_IOCTL_GET_IDLUN:
373 case SCSI_IOCTL_GET_BUS_NUMBER:
376 case SG_GET_RESERVED_SIZE:
377 case SG_SET_RESERVED_SIZE:
378 case SG_EMULATED_HOST:
379 case SCSI_IOCTL_SEND_COMMAND:
380 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
382 return bsg_sg_io(bd->queue, file->f_mode, uarg);
388 static const struct file_operations bsg_fops = {
390 .release = bsg_release,
391 .unlocked_ioctl = bsg_ioctl,
392 .owner = THIS_MODULE,
393 .llseek = default_llseek,
396 void bsg_unregister_queue(struct request_queue *q)
398 struct bsg_class_device *bcd = &q->bsg_dev;
403 mutex_lock(&bsg_mutex);
404 idr_remove(&bsg_minor_idr, bcd->minor);
406 sysfs_remove_link(&q->kobj, "bsg");
407 device_unregister(bcd->class_dev);
408 bcd->class_dev = NULL;
409 mutex_unlock(&bsg_mutex);
411 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
413 int bsg_register_queue(struct request_queue *q, struct device *parent,
414 const char *name, const struct bsg_ops *ops)
416 struct bsg_class_device *bcd;
419 struct device *class_dev = NULL;
422 * we need a proper transport to send commands, not a stacked device
428 memset(bcd, 0, sizeof(*bcd));
430 mutex_lock(&bsg_mutex);
432 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
434 if (ret == -ENOSPC) {
435 printk(KERN_ERR "bsg: too many bsg devices\n");
444 dev = MKDEV(bsg_major, bcd->minor);
445 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
446 if (IS_ERR(class_dev)) {
447 ret = PTR_ERR(class_dev);
450 bcd->class_dev = class_dev;
453 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
455 goto unregister_class_dev;
458 mutex_unlock(&bsg_mutex);
461 unregister_class_dev:
462 device_unregister(class_dev);
464 idr_remove(&bsg_minor_idr, bcd->minor);
466 mutex_unlock(&bsg_mutex);
470 int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
472 if (!blk_queue_scsi_passthrough(q)) {
473 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
477 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
479 EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
481 static struct cdev bsg_cdev;
483 static char *bsg_devnode(struct device *dev, umode_t *mode)
485 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
488 static int __init bsg_init(void)
493 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
494 INIT_HLIST_HEAD(&bsg_device_list[i]);
496 bsg_class = class_create(THIS_MODULE, "bsg");
497 if (IS_ERR(bsg_class))
498 return PTR_ERR(bsg_class);
499 bsg_class->devnode = bsg_devnode;
501 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
503 goto destroy_bsg_class;
505 bsg_major = MAJOR(devid);
507 cdev_init(&bsg_cdev, &bsg_fops);
508 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
510 goto unregister_chrdev;
512 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
513 " loaded (major %d)\n", bsg_major);
516 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
518 class_destroy(bsg_class);
522 MODULE_AUTHOR("Jens Axboe");
523 MODULE_DESCRIPTION(BSG_DESCRIPTION);
524 MODULE_LICENSE("GPL");
526 device_initcall(bsg_init);