Linux 5.1.15
[linux/fpc-iii.git] / block / bsg.c
blobf306853c6b0891a1f59a4643b448b4a03ebb265e
1 /*
2 * bsg.c - block layer implementation of the sg v4 interface
4 * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
5 * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
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>
28 #include <scsi/sg.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__)
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;
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;
55 static int bsg_major;
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)
68 return -EINVAL;
69 return 0;
72 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
73 fmode_t mode)
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");
79 return -EOPNOTSUPP;
82 sreq->cmd_len = hdr->request_len;
83 if (sreq->cmd_len > BLK_MAX_CDB) {
84 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
85 if (!sreq->cmd)
86 return -ENOMEM;
89 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
90 return -EFAULT;
91 if (blk_verify_command(sreq->cmd, mode))
92 return -EPERM;
93 return 0;
96 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
98 struct scsi_request *sreq = scsi_req(rq);
99 int ret = 0;
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);
107 hdr->info = 0;
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,
114 sreq->sense_len);
116 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
117 ret = -EFAULT;
118 else
119 hdr->response_len = len;
122 if (rq_data_dir(rq) == READ)
123 hdr->din_resid = sreq->resid_len;
124 else
125 hdr->dout_resid = sreq->resid_len;
127 return ret;
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)
144 struct request *rq;
145 struct bio *bio;
146 struct sg_io_v4 hdr;
147 int ret;
149 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
150 return -EFAULT;
152 if (!q->bsg_dev.class_dev)
153 return -ENXIO;
155 if (hdr.guard != 'Q')
156 return -EINVAL;
157 ret = q->bsg_dev.ops->check_proto(&hdr);
158 if (ret)
159 return ret;
161 rq = blk_get_request(q, hdr.dout_xfer_len ?
162 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
163 if (IS_ERR(rq))
164 return PTR_ERR(rq);
166 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
167 if (ret)
168 return ret;
170 rq->timeout = msecs_to_jiffies(hdr.timeout);
171 if (!rq->timeout)
172 rq->timeout = q->sg_timeout;
173 if (!rq->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);
186 if (ret)
187 goto out_free_rq;
189 bio = rq->bio;
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);
195 out_free_rq:
196 rq->q->bsg_dev.ops->free_rq(rq);
197 blk_put_request(rq);
198 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
199 return -EFAULT;
200 return ret;
203 static struct bsg_device *bsg_alloc_device(void)
205 struct bsg_device *bd;
207 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
208 if (unlikely(!bd))
209 return NULL;
211 spin_lock_init(&bd->lock);
212 bd->max_queue = BSG_DEFAULT_CMDS;
213 INIT_HLIST_NODE(&bd->dev_list);
214 return bd;
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);
225 return 0;
228 hlist_del(&bd->dev_list);
229 mutex_unlock(&bsg_mutex);
231 bsg_dbg(bd, "tearing down\n");
234 * close can always block
236 kfree(bd);
237 blk_put_queue(q);
238 return 0;
241 static struct bsg_device *bsg_add_device(struct inode *inode,
242 struct request_queue *rq,
243 struct file *file)
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();
254 if (!bd) {
255 blk_put_queue(rq);
256 return ERR_PTR(-ENOMEM);
259 bd->queue = rq;
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);
268 return bd;
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);
280 goto found;
283 bd = NULL;
284 found:
285 return bd;
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));
299 if (!bcd) {
300 bd = ERR_PTR(-ENODEV);
301 goto out_unlock;
304 bd = __bsg_get_device(iminor(inode), bcd->queue);
305 if (!bd)
306 bd = bsg_add_device(inode, bcd->queue, file);
308 out_unlock:
309 mutex_unlock(&bsg_mutex);
310 return bd;
313 static int bsg_open(struct inode *inode, struct file *file)
315 struct bsg_device *bd;
317 bd = bsg_get_device(inode, file);
319 if (IS_ERR(bd))
320 return PTR_ERR(bd);
322 file->private_data = bd;
323 return 0;
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)
341 int queue;
343 if (get_user(queue, uarg))
344 return -EFAULT;
345 if (queue < 1)
346 return -EINVAL;
348 spin_lock_irq(&bd->lock);
349 bd->max_queue = queue;
350 spin_unlock_irq(&bd->lock);
351 return 0;
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;
359 switch (cmd) {
361 * Our own ioctls
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);
369 * SCSI/sg ioctls
371 case SG_GET_VERSION_NUM:
372 case SCSI_IOCTL_GET_IDLUN:
373 case SCSI_IOCTL_GET_BUS_NUMBER:
374 case SG_SET_TIMEOUT:
375 case SG_GET_TIMEOUT:
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);
381 case SG_IO:
382 return bsg_sg_io(bd->queue, file->f_mode, uarg);
383 default:
384 return -ENOTTY;
388 static const struct file_operations bsg_fops = {
389 .open = bsg_open,
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;
400 if (!bcd->class_dev)
401 return;
403 mutex_lock(&bsg_mutex);
404 idr_remove(&bsg_minor_idr, bcd->minor);
405 if (q->kobj.sd)
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;
417 dev_t dev;
418 int ret;
419 struct device *class_dev = NULL;
422 * we need a proper transport to send commands, not a stacked device
424 if (!queue_is_mq(q))
425 return 0;
427 bcd = &q->bsg_dev;
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);
433 if (ret < 0) {
434 if (ret == -ENOSPC) {
435 printk(KERN_ERR "bsg: too many bsg devices\n");
436 ret = -EINVAL;
438 goto unlock;
441 bcd->minor = ret;
442 bcd->queue = q;
443 bcd->ops = ops;
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);
448 goto idr_remove;
450 bcd->class_dev = class_dev;
452 if (q->kobj.sd) {
453 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
454 if (ret)
455 goto unregister_class_dev;
458 mutex_unlock(&bsg_mutex);
459 return 0;
461 unregister_class_dev:
462 device_unregister(class_dev);
463 idr_remove:
464 idr_remove(&bsg_minor_idr, bcd->minor);
465 unlock:
466 mutex_unlock(&bsg_mutex);
467 return ret;
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");
474 return -EINVAL;
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)
490 int ret, i;
491 dev_t devid;
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");
502 if (ret)
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);
509 if (ret)
510 goto unregister_chrdev;
512 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
513 " loaded (major %d)\n", bsg_major);
514 return 0;
515 unregister_chrdev:
516 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
517 destroy_bsg_class:
518 class_destroy(bsg_class);
519 return ret;
522 MODULE_AUTHOR("Jens Axboe");
523 MODULE_DESCRIPTION(BSG_DESCRIPTION);
524 MODULE_LICENSE("GPL");
526 device_initcall(bsg_init);