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
36struct 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
48static DEFINE_MUTEX(bsg_mutex);
49static DEFINE_IDR(bsg_minor_idr);
50
51#define BSG_LIST_ARRAY_SIZE 8
52static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
53
54static struct class *bsg_class;
55static int bsg_major;
56
57static 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
64static 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
72static 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 if (hdr->dout_xfer_len && hdr->din_xfer_len) {
78 pr_warn_once("BIDI support in bsg has been removed.\n");
79 return -EOPNOTSUPP;
80 }
81
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;
87 }
88
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;
94}
95
96static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
97{
98 struct scsi_request *sreq = scsi_req(rq);
99 int ret = 0;
100
101 /*
102 * fill in all the output members
103 */
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;
111
112 if (sreq->sense_len && hdr->response) {
113 int len = min_t(unsigned int, hdr->max_response_len,
114 sreq->sense_len);
115
116 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
117 ret = -EFAULT;
118 else
119 hdr->response_len = len;
120 }
121
122 if (rq_data_dir(rq) == READ)
123 hdr->din_resid = sreq->resid_len;
124 else
125 hdr->dout_resid = sreq->resid_len;
126
127 return ret;
128}
129
130static void bsg_scsi_free_rq(struct request *rq)
131{
132 scsi_req_free_cmd(scsi_req(rq));
133}
134
135static 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,
140};
141
142static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
143{
144 struct request *rq;
145 struct bio *bio;
146 struct sg_io_v4 hdr;
147 int ret;
148
149 if (copy_from_user(&hdr, uarg, sizeof(hdr)))
150 return -EFAULT;
151
152 if (!q->bsg_dev.class_dev)
153 return -ENXIO;
154
155 if (hdr.guard != 'Q')
156 return -EINVAL;
157 ret = q->bsg_dev.ops->check_proto(&hdr);
158 if (ret)
159 return ret;
160
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);
165
166 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
167 if (ret)
168 return ret;
169
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;
177
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);
184 }
185
186 if (ret)
187 goto out_free_rq;
188
189 bio = rq->bio;
190
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);
194
195out_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;
201}
202
203static struct bsg_device *bsg_alloc_device(void)
204{
205 struct bsg_device *bd;
206
207 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
208 if (unlikely(!bd))
209 return NULL;
210
211 spin_lock_init(&bd->lock);
212 bd->max_queue = BSG_DEFAULT_CMDS;
213 INIT_HLIST_NODE(&bd->dev_list);
214 return bd;
215}
216
217static int bsg_put_device(struct bsg_device *bd)
218{
219 struct request_queue *q = bd->queue;
220
221 mutex_lock(&bsg_mutex);
222
223 if (!refcount_dec_and_test(&bd->ref_count)) {
224 mutex_unlock(&bsg_mutex);
225 return 0;
226 }
227
228 hlist_del(&bd->dev_list);
229 mutex_unlock(&bsg_mutex);
230
231 bsg_dbg(bd, "tearing down\n");
232
233 /*
234 * close can always block
235 */
236 kfree(bd);
237 blk_put_queue(q);
238 return 0;
239}
240
241static struct bsg_device *bsg_add_device(struct inode *inode,
242 struct request_queue *rq,
243 struct file *file)
244{
245 struct bsg_device *bd;
246 unsigned char buf[32];
247
248 lockdep_assert_held(&bsg_mutex);
249
250 if (!blk_get_queue(rq))
251 return ERR_PTR(-ENXIO);
252
253 bd = bsg_alloc_device();
254 if (!bd) {
255 blk_put_queue(rq);
256 return ERR_PTR(-ENOMEM);
257 }
258
259 bd->queue = rq;
260
261 refcount_set(&bd->ref_count, 1);
262 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
263
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);
267
268 return bd;
269}
270
271static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
272{
273 struct bsg_device *bd;
274
275 lockdep_assert_held(&bsg_mutex);
276
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;
281 }
282 }
283 bd = NULL;
284found:
285 return bd;
286}
287
288static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
289{
290 struct bsg_device *bd;
291 struct bsg_class_device *bcd;
292
293 /*
294 * find the class device
295 */
296 mutex_lock(&bsg_mutex);
297 bcd = idr_find(&bsg_minor_idr, iminor(inode));
298
299 if (!bcd) {
300 bd = ERR_PTR(-ENODEV);
301 goto out_unlock;
302 }
303
304 bd = __bsg_get_device(iminor(inode), bcd->queue);
305 if (!bd)
306 bd = bsg_add_device(inode, bcd->queue, file);
307
308out_unlock:
309 mutex_unlock(&bsg_mutex);
310 return bd;
311}
312
313static int bsg_open(struct inode *inode, struct file *file)
314{
315 struct bsg_device *bd;
316
317 bd = bsg_get_device(inode, file);
318
319 if (IS_ERR(bd))
320 return PTR_ERR(bd);
321
322 file->private_data = bd;
323 return 0;
324}
325
326static int bsg_release(struct inode *inode, struct file *file)
327{
328 struct bsg_device *bd = file->private_data;
329
330 file->private_data = NULL;
331 return bsg_put_device(bd);
332}
333
334static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
335{
336 return put_user(bd->max_queue, uarg);
337}
338
339static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
340{
341 int queue;
342
343 if (get_user(queue, uarg))
344 return -EFAULT;
345 if (queue < 1)
346 return -EINVAL;
347
348 spin_lock_irq(&bd->lock);
349 bd->max_queue = queue;
350 spin_unlock_irq(&bd->lock);
351 return 0;
352}
353
354static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
355{
356 struct bsg_device *bd = file->private_data;
357 void __user *uarg = (void __user *) arg;
358
359 switch (cmd) {
360 /*
361 * Our own ioctls
362 */
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);
367
368 /*
369 * SCSI/sg ioctls
370 */
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;
385 }
386}
387
388static 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,
394};
395
396void bsg_unregister_queue(struct request_queue *q)
397{
398 struct bsg_class_device *bcd = &q->bsg_dev;
399
400 if (!bcd->class_dev)
401 return;
402
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);
410}
411EXPORT_SYMBOL_GPL(bsg_unregister_queue);
412
413int bsg_register_queue(struct request_queue *q, struct device *parent,
414 const char *name, const struct bsg_ops *ops)
415{
416 struct bsg_class_device *bcd;
417 dev_t dev;
418 int ret;
419 struct device *class_dev = NULL;
420
421 /*
422 * we need a proper transport to send commands, not a stacked device
423 */
424 if (!queue_is_mq(q))
425 return 0;
426
427 bcd = &q->bsg_dev;
428 memset(bcd, 0, sizeof(*bcd));
429
430 mutex_lock(&bsg_mutex);
431
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;
437 }
438 goto unlock;
439 }
440
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;
449 }
450 bcd->class_dev = class_dev;
451
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;
456 }
457
458 mutex_unlock(&bsg_mutex);
459 return 0;
460
461unregister_class_dev:
462 device_unregister(class_dev);
463idr_remove:
464 idr_remove(&bsg_minor_idr, bcd->minor);
465unlock:
466 mutex_unlock(&bsg_mutex);
467 return ret;
468}
469
470int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
471{
472 if (!blk_queue_scsi_passthrough(q)) {
473 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
474 return -EINVAL;
475 }
476
477 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
478}
479EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
480
481static struct cdev bsg_cdev;
482
483static char *bsg_devnode(struct device *dev, umode_t *mode)
484{
485 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
486}
487
488static int __init bsg_init(void)
489{
490 int ret, i;
491 dev_t devid;
492
493 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
494 INIT_HLIST_HEAD(&bsg_device_list[i]);
495
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;
500
501 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
502 if (ret)
503 goto destroy_bsg_class;
504
505 bsg_major = MAJOR(devid);
506
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;
511
512 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
513 " loaded (major %d)\n", bsg_major);
514 return 0;
515unregister_chrdev:
516 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
517destroy_bsg_class:
518 class_destroy(bsg_class);
519 return ret;
520}
521
522MODULE_AUTHOR("Jens Axboe");
523MODULE_DESCRIPTION(BSG_DESCRIPTION);
524MODULE_LICENSE("GPL");
525
526device_initcall(bsg_init);
527